Python Forum

Full Version: Pick Value from Json
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

I have a below JSON output. I want to pick the value of "Id" from the JSON result.

[
{
"Attributes": {
"accessories": [],
"age": 32.0,
"blur": {
"blurLevel": "low",
"value": 0.11
}
"occlusion": {
"eyeOccluded": false,
"foreheadOccluded": false,
"mouthOccluded": false
},

"Id": "c8c21d40",
"faceRectangle": {
"height": 154,
"left": 477,
"top": 144,
"width": 154
}
}
]
is this a valid json ?
Not at all.
I fixed your json, since what you posted isn't valid. So the path to the id might not be the same as whatever the real data is. This should be enough to get you going in the right direction, though.

Also, after this post, I'm going to move the thread somewhere more applicable, since this has nothing to do with Python News lol

>>> import json
>>> text = '''
... {
... "Attributes": {
... "accessories": [],
... "age": 32.0,
... "blur": {
... "blurLevel": "low",
... "value": 0.11
... },
... "occlusion": {
... "eyeOccluded": false,
... "foreheadOccluded": false,
... "mouthOccluded": false
... },
...
... "Id": "c8c21d40",
... "faceRectangle": {
... "height": 154,
... "left": 477,
... "top": 144,
... "width": 154
... }
... }
... }
... '''
>>> parsed = json.loads(text)
>>> parsed
{'Attributes': {'accessories': [], 'age': 32.0, 'blur': {'blurLevel': 'low', 'value': 0.11}, 'occlusion': {'eyeOccluded': False, 'foreheadOccluded': False, 'mouthOccluded': False}, 'Id': 'c8c21d40', 'faceRectangle': {'height': 154, 'left': 477, 'top': 144, 'width': 154}}}
>>> parsed['Attributes']['Id']
'c8c21d40'