Python Forum

Full Version: save json value
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I have to search a value in a json and store it in a variable.

Its easy but the problem is that the json has a primary 'data' tag before the tag with the value I need.

{"data":[{"name":"UVC G3 Dome"

When I do a:
return GETReturn['name']
I have the following error: KeyError: 'name'

Can you tell me how to look for sub-values please.

Thank you
In the dictionary you get back from JSON,there is a mix of dictionaries and list.
So use [0] to access the dictionary inside list.
>>> d = {"data":[{"name":"UVC G3 Dome"}]}
>>> d['data']
[{'name': 'UVC G3 Dome'}]
>>> d['data'][0]['name']
'UVC G3 Dome'
Perfect

Thank you snippsat

I take this opportunity to continue the question if I am behind two elements.

if i want to get the value of 'motionRecordEnabled' please

{"data":[{"recordingSettings":{"motionRecordEnabled":false,"fullTimeRecordEnabled":false,"channel":"0","prePaddingSecs":0,"postPaddingSecs":0,"storagePath":null}

thank you

It's ok :)

Subject resolved

thk
It's the same way to navigate,if i fix the data so i can run it.
>>> d['data'][0]['recordingSettings']['motionRecordEnabled']
'false'