Reputation: 49
COMPLEX JSON GET VALUE in Python? I`m trying to extract the value 9 but i dont get it. How to extract the value of 9? I tried this:
data = json.loads(string)
data['fiednf']['0']['idk'])
but doesn`t work.
{
"ABC":"ABC",
"EDF":{
"name":"name",
"EDF":true
},
"GHJ":"",
"FG":"geometryType",
"GH":{
"RT":wkid,
"ED":latestWkid
},
"editval":[
{
"nome":"name",
"tipo":"type",
"alias":"alias",
"sqlType":"sqlType",
"domain":null,
"defaultValue":null
}
],
"fiednf":[
{
"numbers":{
"idk":9
}
}
]
}
Upvotes: 1
Views: 80
Reputation: 123393
I think this will do it:
print(data["fiednf"][0]["numbers"]["idk"]) # -> 9
data["fiednf"]
is a list and their contents are generally referenced via integer indices. In this case you want the first (and only) element in the list, so an index of [0]
must be used.
I often find it helpful to pretty-print the data to see its structure better. In this case you can do with:
print(json.dumps(data, indent=4))
The result in this case looked something like this:
{
"ABC": "ABC",
"EDF": {
"name": "name",
"EDF": true
},
"GHJ": "",
"FG": "geometryType",
"GH": {
"RT": "wkid",
"ED": "latestWkid"
},
"editval": [
{
"nome": "name",
"tipo": "type",
"alias": "alias",
"sqlType": "sqlType",
"domain": null,
"defaultValue": null
}
],
"fiednf": [
{
"numbers": {
"idk": 9
}
}
]
}
Upvotes: 2
Reputation: 4510
To access 9 you can do this:
print(a['fiednf'][0]['numbers']['idk'])
>>> 9
You only use string indexes for dictionaries and int indexes for lists, also the key fiednf
is duplicated.
Upvotes: 2