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
JavaScript
x
4
1
a = json.dumps({"Name": "Robert",
2
"Date" : "January 17th, 2017",
3
"Address" : "Jakarta"})
4
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
JavaScript
1
4
1
Name
2
Date
3
Address
4
Advertisement
Answer
You must convert json
to dict
and then the labels
are the same as the keys
.
JavaScript
1
7
1
import json
2
a = json.dumps({"Name": "Robert",
3
"Date" : "January 17th, 2017",
4
"Address" : "Jakarta"})
5
for key in json.loads(a):
6
print(key)
7
output:
JavaScript
1
4
1
Name
2
Date
3
Address
4
Optional:
If you want to access the values of each item
JavaScript
1
8
1
import json
2
a = json.dumps({"Name": "Robert",
3
"Date" : "January 17th, 2017",
4
"Address" : "Jakarta"})
5
d = json.loads(a)
6
for key in d:
7
print("key: {}, value: {}".format(key, d[key]))
8
Python2
JavaScript
1
3
1
for key, value in json.loads(a).iteritems():
2
print("key: {}, value: {}".format(key, value))
3
Python3
JavaScript
1
3
1
for key, value in json.loads(a).items():
2
print("key: {}, value: {}".format(key, value))
3
Output:
JavaScript
1
4
1
key: Name, value: Robert
2
key: Date, value: January 17th, 2017
3
key: Address, value: Jakarta
4