Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
save json value
#1
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
Reply
#2
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'
Reply
#3
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
Reply
#4
It's the same way to navigate,if i fix the data so i can run it.
>>> d['data'][0]['recordingSettings']['motionRecordEnabled']
'false'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to save to multiple locations during save cubangt 1 566 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  How to save json data in a dataframe shantanu97 1 2,169 Apr-15-2021, 02:44 PM
Last Post: klllmmm
  json.dumps save some unicode chars as code, not char buran 1 2,926 Aug-02-2018, 04:02 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020