I’m a beginner when it comes to Python and I’m trying to learn how to use data structures such as JSON objects but I’m kind of stuck trying to actually get the data from the JSON object.
This is the contents example JSON data file.
{"data":{"internalName":"value","int":1}}
I’m able to print the data in the file through the code I already have, but I want to print only a certain value like the value of the internalName. How would I print this using the code I already have?
import json
json_data=open('data.txt')
data = json.load(json_data)
print json.dumps(data)
json_data.close()
You can treat the resulting data structure just like a dictionary. In this case, you have have a
keycalleddatainside of the structure, and thevaluefor that key is another dictionary, which has two keys:internalNameandint. In order to access the values, you can use the syntax in the following example:So in your case, after you define
data, you can access it in a similar way. Also, I would suggest usingwithto open the file as it will handle closing for you (when you leave the indented block):