Jeremy Barnes Jeremy Barnes - 2 months ago 8
JSON Question

How to extract a specific part from a JSON response?

I have code like

import requests
dict_api = "http://api.pearson.com/v2/dictionaries/entries?headword="
r = requests.get(dict_api + word)
results = r.json()["results"]


and this gives me this:

[
{
"senses": [
{
"translation": "\\u6c99\\u62c9\\uff0c\\u8272\\u62c9\\uff0c\\u6c99\\u5f8b\\u3014\\u7528\\u751f\\u83dc\\u3001\\u9ec3\\u74dc\\u3001\\u756a\\u8304\\u7b49\\u751f\\u7684\\u852c\\u83dc\\u4f5c\\u539f\\u6599\\u7684\\u6dbc\\u62cc\\u83dc\\u3015"
}
],
"url": "/v2/dictionaries/entries/cqAJbKG6Bx",
"part_of_speech": "noun",
"datasets": [
"ldec",
"dictionary"
],
"headword": "salad",
"id": "cqAJbKG6Bx"
},
{
"pronunciations": [
{
"ipa": "\\u02c8s\u5adcu0259d",
"audio": [
{
"url": "/v2/dictionaries/assets/ldoce/gb_pron/salad_las2_br.mp3",
"lang": "British English",
"type": "pronunciation"
},
{
"url": "/v2/dictionaries/assets/ldoce/us_pron/salad.mp3",
"lang": "American English",
"type": "pronunciation"
}
]
}
],
"senses": [
{
"definition": [
"a mixture of raw vegetables, especially lettuce, cucumber, and tomato"
],
"collocation_examples": [
{
"collocation": "toss a salad",
"example": {
"audio": [
{
"url": "/v2/dictionaries",
"type": "example"
}
]
}
}
],
"examples": [
{
"text": "Would you like some salad with your pasta?",
"audio": [
{
"url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-000741011.mp3",
"type": "example"
}
]
}
]
}
],
"url": "/v2/dictionaries/entries/cqAFq6n9JR",
"part_of_speech": "noun",
"datasets": [
"ldoce5",
"dictionary"
],
"headword": "salad",
"id": "cqAFq6n9JR"
},
{
"senses": [
{
"definition": "a mixture of vegetables, eaten cold",
"examples": [
{
"text": "a tomato salad"
}
]
}
],
"url": "/v2/dictionaries/entries/cqARFW4JGH",
"part_of_speech": "noun",
"datasets": [
"wordwise",
"dictionary"
],
"headword": "salad",
"id": "cqARFW4JGH"
},
{
"pronunciations": [
{
"ipa": "\\u02c8s\u5adcu0259d"
}
],
"senses": [
{}
],
"url": "/v2/dictionaries/entries/csTMd1V83W",
"part_of_speech": "noun",
"datasets": [
"laes",
"dictionary"
],
"headword": "salad",
"id": "csTMd1V83W"
},
{
"senses": [
{
"definition": "rou groentemengsel",
"translations": [
{
"text": "slaai"
}
]
}
],
"url": "/v2/dictionaries/entries/cqAG06X6Kc",
"part_of_speech": "noun",
"datasets": [
"leasd",
"dictionary"
],
"headword": "salad",
"id": "cqAG06X6Kc"
},
{
"senses": [
{
"definition": "a mixture of raw vegetables, usually including lettuce",
"examples": [
{
"text": "a tomato and cucumber salad"
}
]
}
],
"url": "/v2/dictionaries/entries/cs1tApKyAq",
"part_of_speech": "noun",
"datasets": [
"laad3",
"dictionary"
],
"headword": "salad",
"id": "cs1tApKyAq"
},
{
"pronunciations": [
{
"lang": "American English",
"ipa": "\\u02c8s\u5adcu0259d"
}
],
"senses": [
{
"definition": [
"dish of mixed raw vegetables"
],
"translations": [
{
"text": [
"salada"
],
"example": [
{
"text": "a salad of tomatoes, lettuce and cucumber",
"translation": {
"text": [
"uma salada de tomate, alface e pepino"
]
}
}
]
}
]
}
],
"url": "/v2/dictionaries/entries/ct59xTBDRn",
"part_of_speech": "noun",
"datasets": [
"brep",
"dictionary"
],
"headword": "salad",
"id": "ct59xTBDRn"
},
{
"pronunciations": [
{
"ipa": "\\u02c8s\u5adcu0259d"
}
],
"senses": [
{
"definition": [
"a mixture of vegetables eaten cold and usually raw"
],
"examples": [
{
"text": "a salad of lettuce, tomatoes, and cucumber"
}
]
}
],
"url": "/v2/dictionaries/entries/cvfdktDye8",
"part_of_speech": "noun",
"datasets": [
"lasde",
"dictionary"
],
"headword": "salad",
"id": "cvfdktDye8"
},
{
"senses": [
{
"definition": "a salad made with lettuce and other raw green vegetables"
}
],
"url": "/v2/dictionaries/entries/cs1saJp1NK",
"part_of_speech": "noun",
"datasets": [
"laad3",
"dictionary"
],
"headword": "green salad",
"id": "cs1saJp1NK"
},
{
"senses": [
{
"translation": "\\u8a00\\u8a9e\\u96dc\\u4e82\\uff0c\\u8a00\\u8a9e\\u856a\\u96dc"
}
],
"url": "/v2/dictionaries/entries/cqAJqE3gvE",
"part_of_speech": "noun",
"datasets": [
"ldec",
"dictionary"
],
"headword": "word salad",
"id": "cqAJqE3gvE"
}
]


All I want to do is get each 'part_of_speech' value from the response and push it into a list, but I can't figure out how to do this.

I have unsuccessfully tried parsing and python insists that this is actually a list.

Answer
print(results[0]['part_of_speech'])

#prints 'noun'
Comments