简体   繁体   中英

Parsing nested JSON objects with Python

I have the following JSON data and would like to extract email value using Python:

{
  "_links": {
"self": {
  "href": "https://example.com/comments/9"
}
  },
"_embedded": {
"customer": {
  "name": "Jamie XXXX",
  "email": "jamie@example.tv",
  "thumbnail": {
    "small": "https://secure.gravatar.com/avatar/dfd.png?d=blank&r=PG&s=100",
    "medium": "https://secure.gravatar.com/avatar/dfd.png?d=blank&r=PG&s=200",
    "large": "https://secure.gravatar.com/avatar/dfdfd.png?d=blank&r=PG&s=300"
  }
},
"comments": []
},
"id": 9,
"video_id": null,
"content": "j/k I meant that as a reply",
"comments_count": 0,
"created_at": "2014-03-12T17:46:07Z",
"updated_at": "2014-03-12T17:46:07Z"
}

I tried something like but it's not working:

jsonresp = r.json()
for k, v in jsonresp:
     print(jsonresp['_embedded']['customer']['email'])
jsonresp = r.json()
print(jsonresp['_embedded']['customer']['email'])

Using pyjq :

import json
import pyjq

with open("input.json", "r") as myfile:
    data=json.load(myfile)

print pyjq.first('._embedded.customer.email', data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM