Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a json file stored multiple libraries. Just want to know how should I get the value for a specific key from diff libraries. For example, each library contain info about a meal, and I want to get names for every meal(name as a key).

What I have tried:

recipe = []
with open('maryCooks_allergen=salt.json', 'r') as fh:
s = fh.read()
recipe_dictionary = json.loads(s)

for i in recipe_dictionary:
for key,value in i.items():
print(value)
Posted
Updated 28-Nov-20 21:37pm

1 solution

You need to itrate through the dictionary post loading the json and then go through it's items to extract key or value.

Try something like this:
Python
import json

# print values v if those are needed
def print_keys(mapping):
    [print('key:',k) for k, v in mapping.items()]
    return
    
# some JSON:
x = '{ "name":"John Doe", "age":30, "city":"New York"}'

# parse x:
y = json.loads(x)

# all it's keys
print_keys(y)

#Output
#key: name
#key: age
#key: city
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900