![]() |
save json value - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: save json value (/thread-12427.html) |
save json value - DavidFernandez - Aug-24-2018 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 RE: save json value - snippsat - Aug-24-2018 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' RE: save json value - DavidFernandez - Aug-24-2018 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 RE: save json value - snippsat - Aug-24-2018 It's the same way to navigate,if i fix the data so i can run it. >>> d['data'][0]['recordingSettings']['motionRecordEnabled'] 'false' |