json problem - 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: json problem (/thread-23259.html) |
json problem - enigma619 - Dec-18-2019 Hi I'm new on this forum. I'm working with json and python and i don't succeed to retrieve some information... my json: I'm trying to print only: "nom" but don't succeed in it.My code: with open('/tmp/file.json') as json_file1: data = json.load(json_file) for key, value in data.items(): variable = data[domaine]I only success to print only domaine, or all my json, not just "nom" Sorry for this perhaps simply question.. Thanks for help Alex RE: json problem - Clunk_Head - Dec-18-2019 Put this into your for loop so you can examine what you are dealing with. print("Key:", key, ",Value:", value) RE: json problem - ndc85430 - Dec-18-2019 Line 5: the variable domaine isn't declared anywhere is it? Also, don't you think something must be wrong if you aren't using your loop variables (i.e. key and value ) in the loop?
RE: json problem - enigma619 - Dec-18-2019 #!/usr/bin/env python3 import json import sys import os with open('/tmp/config1.json') as json_file: data = json.load(json_file) for key, value in data.items(): print ("key:", key,) print ("value", value,)And result is: My exact json is: I've created a new python prog to be sure domaine and others are not already declared.Thanks RE: json problem - Clunk_Head - Dec-18-2019 Excellent, like the replies that helped and mark the thread as solved. RE: json problem - enigma619 - Dec-18-2019 Hum no it's not solved :) In fact I want to only print "nom" (just that key, not the contain) But I don't know how to retrieve just that. RE: json problem - Clunk_Head - Dec-18-2019 for key in data: print(key) To pull the key/value pair from a dictionary you need to use the dictionary_name.items() To pull just the keys you only need the dictionary_name RE: json problem - enigma619 - Dec-19-2019 Seems doesn't work too :/ In this case: #!/usr/bin/env python3 import json import sys with open('/tmp/config1.json') as json_file1: data = json.load(json_file1) for key in data: print(key)Result is: In my case what is interesting for me is: " To pull the key/value pair from a dictionary you need to use the dictionary_name.items()" Because, for the moment, here I need to retreive nom.But next, in my script I will need other informations. I continue to search but for the moment no success .. :( RE: json problem - buran - Dec-19-2019 It's a nested structure. The example will result in JSON object that is dict of dicts of dicts. The most appropriate approach would depend on whether you know in advance what it looks like, e.g. how many levels, how many (i.e. one or many/undefined number) elements in each level. Most generic approach: import json with open('sample.json') as json_file: data = json.load(json_file) for key, value in data.items(): print(f'key: {key}') for key2, value2 in value.items(): print(f'key2: {key2}') for key3, value3 in value2.items(): print(f'key3: {key3}') print(f'value3: {value3}') # in the example value3 is a list, i.e. you can iterate over/access it's elements In my code I use generic key, key2, key3/value, bvalue2, value3 variable names. You better use more descriptive names if possible
RE: json problem - enigma619 - Dec-19-2019 Yeah :) Thanks for explanation and informations, all is working fine now. Have a good day :) Alex |