Python Forum

Full Version: parser json response
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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":
this is not valid json. please, post valid one
Also, make sure to post enough code to demonstrate the problem (where is jsonResponse declared?) and error messages as appropriate.
(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?
(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']