If you want to iterate over both keys and values of the dictionary, do this:
for key, value in data.items():
print(key, value)
Answer from Lior on Stack OverflowIf you want to iterate over both keys and values of the dictionary, do this:
for key, value in data.items():
print(key, value)
What error is it giving you?
If you do exactly this:
data = json.loads('{"lat":444, "lon":555}')
Then:
data['lat']
SHOULD NOT give you any error at all.
Json.loads only returns names but not the values
JSON get value by keys path (string)
Advice on how to iterate through JSON to find the first instance of a key value pair?
How to get values from all instances of key in JSON in Python - Stack Overflow
Wondering if there’s a way to do this with the standard json module. Looking to just extract the value of 1 key from a large json file without needing to load the whole thing
For example, I had a json file with:
{
“x”: …,
“y”: …,
“z”: …
}is there a way of doing something like json.load(…) but only reading everything under key “y” for example?
I have a JSON file that I want to automatically check specific values, like myJSON['generic']['lessgeneric']['store'] should be equal to 100,
was thinking of doing something like this:
checks = [ {'name':'field_1','path':'myJSON['generic']['lessgeneric']['store']','value':'100'} ]
but I have no clue how to convert the string "myJSON['generic']['lessgeneric']['store']" into it's value, any help would be appreciated!
edit: how I solved it
path = "generic>lessgeneric>store"
value = 100
def check_value(path,value):
temp_json = my_json
for key in path.split(">"):
temp_json = temp_json[key]
if temp_json == value:
passI have an undesirable JSON object that I can't modify:
{
"programs": [
],
"streams": [
{
"index": 0,
"codec_name": "hevc",
"codec_type": "video"
},
{
"index": 1,
"codec_name": "aac",
"codec_type": "audio"
},
{
"index": 2,
"codec_name": "opus",
"codec_type": "audio"
},
{
"index": 3,
"codec_name": "ac3",
"codec_type": "audio"
},
{
"index": 4,
"codec_name": "ass",
"codec_type": "subtitle"
},
{
"index": 5,
"codec_name": "ttf",
"codec_type": "attachment"
}
]
}This is psudo json from the very real output of ffprobe -loglevel error -show_entries format:stream=index,stream,codec_type,codec_name -of json FILENAME
I need to get the first codec_name from the lowest index of the codec_type: audio.
In this case, index 1, 2, and 3 are all of codec_type: audio, so the lowest index/first instance would be 1 and my codec_name would be aac.
Any ideas on how to move forward on a problem like this? I can't seem to find any stackoverflow threads with anything similar.
----------------------------
EDIT, solution here: https://www.reddit.com/r/learnpython/comments/16af8zf/comment/jz74hc9/?utm_source=share&utm_medium=web2x&context=3
Thank you u/shiftybyte, u/djshadesuk!!!
You are getting that error because you cannot treat JSON as a dictionary yet, you must load it first
import json
response = json.loads(JSON_RESPONSE)
all_C_values = [dic['c'] for dic in response]
Try this:
D = [ { "a": 3, "b": 2, "c": 1, }, { "a": 3, "b": 2, "c": 1 } ]
x = [x['c'] for x in D]
print(x)