i am a newbie on JSON, I want to get just ‘label’ from my JSON data here is my JSON data, i am using json.dumps to turn it into JSON format
a = json.dumps({"Name": "Robert",
"Date" : "January 17th, 2017",
"Address" : "Jakarta"})
I want to print just the label of my JSON data, is there any possible way to do it? The result that i want is
Name Date Address
Advertisement
Answer
You must convert json to dict and then the labels are the same as the keys.
import json
a = json.dumps({"Name": "Robert",
"Date" : "January 17th, 2017",
"Address" : "Jakarta"})
for key in json.loads(a):
print(key)
output:
Name Date Address
Optional:
If you want to access the values of each item
import json
a = json.dumps({"Name": "Robert",
"Date" : "January 17th, 2017",
"Address" : "Jakarta"})
d = json.loads(a)
for key in d:
print("key: {}, value: {}".format(key, d[key]))
Python2
for key, value in json.loads(a).iteritems():
print("key: {}, value: {}".format(key, value))
Python3
for key, value in json.loads(a).items():
print("key: {}, value: {}".format(key, value))
Output:
key: Name, value: Robert key: Date, value: January 17th, 2017 key: Address, value: Jakarta