Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
parser json response
#1
Hi!

I'm trying to traslate shell script bash to pyhton. I use jq to extract fileds of json response but with jsonresponse I can't do it with python. Someone knows? My bash line

jq -r '.search_objects[].title' file
I'm trying with

print(jsonResponse["search_objects"]["title"])
But fails :-(

The json response is:

{"search_objects":[{"id":"wzyn2v2k22j5","title":"juego party and co","description":"totalmente nuevo,nunca se ha usado ","distance":497.0,"images":[{"original":"http://cdn.wallapop.com/imag
es/10420/90/wu/__/c10420p545727668/i1624356241.jpg?pictureSize=W1024","xsmall":"http://cdn.wallapop.com/images/10420/90/wu/__/c10420p545727668/i1624356241.jpg?pictureSize=W320","small":"htt
p://cdn.wallapop.com/images/10420/90/wu/__/c10420p545727668/i1624356241.jpg?pictureSize=W320","large":"http://cdn.wallapop.com/images/10420/90/wu/__/c10420p545727668/i1624356241.jpg?picture
Size=W800","medium":"http://cdn.wallapop.com/images/10420/90/wu/__/c10420p545727668/i1624356241.jpg?pictureSize=W640","xlarge":"http://cdn.wallapop.com/images/10420/90/wu/__/c10420p54572766
8/i1624356241.jpg?pictureSize=W1024","original_width":1200,"original_height":554}],"user":{"id":"4w671w9yp86x","micro_name":"sonia m.","image":{"original":"http://cdn.wallapop.com/images/13
/2v/lu/__/c13p174019176/i1600856774.jpg?pictureSize=W1024","xsmall":
Reply
#2
this is not valid json. please, post valid one
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Also, make sure to post enough code to demonstrate the problem (where is jsonResponse declared?) and error messages as appropriate.
Reply
#4
(Sep-15-2020, 11:27 AM)ndc85430 Wrote: Also, make sure to post enough code to demonstrate the problem (where is jsonResponse declared?) and error messages as appropriate.
The error message is:
Entire JSON response
Other error occurred: list indices must be integers or slices, not str
(Sep-15-2020, 11:23 AM)buran Wrote: this is not valid json. please, post valid one

Jq works. I think is json file right?
Reply
#5
(Sep-15-2020, 11:47 AM)absolut Wrote: I think is json file right?
No, even after removing the extra new-lines in what you post - it's incomplete
Output:
{ "search_objects": [{ "id": "wzyn2v2k22j5", "title": "juego party and co", "description": "totalmente nuevo,nunca se ha usado ", "distance": 497.0, "images": [{ "original": "http://cdn.wallapop.com/images/10420/90/wu/__/c10420p545727668/i1624356241.jpg?pictureSize=W1024", "xsmall": "http://cdn.wallapop.com/images/10420/90/wu/__/c10420p545727668/i1624356241.jpg?pictureSize=W320", "small": "http://cdn.wallapop.com/images/10420/90/wu/__/c10420p545727668/i1624356241.jpg?pictureSize=W320", "large": "http://cdn.wallapop.com/images/10420/90/wu/__/c10420p545727668/i1624356241.jpg?pictureSize=W800", "medium": "http://cdn.wallapop.com/images/10420/90/wu/__/c10420p545727668/i1624356241.jpg?pictureSize=W640", "xlarge": "http://cdn.wallapop.com/images/10420/90/wu/__/c10420p545727668/i1624356241.jpg?pictureSize=W1024", "original_width": 1200, "original_height": 554 }], "user": { "id": "4w671w9yp86x", "micro_name": "sonia m.", "image": { "original": "http://cdn.wallapop.com/images/13/2v/lu/__/c13p174019176/i1600856774.jpg?pictureSize=W1024", "xsmall":
In any case, it's obvious that the value of key 'search_objects' is a list of dicts. So you need to iterate over each element in that list (i.e. each dict in the list) and then search for key title in that dict. or access specific element by index (i.e. int index), e.g.
json_data['search_objects'][0]['title']
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,193 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  Name not found in response json NewbiePyt 4 1,023 Dec-29-2022, 11:12 AM
Last Post: buran
  JSON response from REST service get nested value nl2ttl 2 2,489 Nov-30-2020, 09:34 PM
Last Post: nl2ttl
  Empty response to request causing .json() to error t4keheart 1 9,953 Jun-26-2020, 08:35 PM
Last Post: bowlofred
  API JSON response missing list gives keyerror rolfmadsen 3 3,402 Mar-28-2020, 10:12 AM
Last Post: buran
  How to process JSON response from requests? Heinrich 5 3,962 Jan-10-2020, 06:17 PM
Last Post: Heinrich
  Split of key from JSON response aswini_dubey 1 2,364 Dec-04-2019, 06:54 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020