3

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
1
  • 2
    Once this is JSON, this becomes difficult. Why don't you just use the Python data structure? JSON here makes no sense. Commented Jan 16, 2017 at 23:26

4 Answers 4

4

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
Sign up to request clarification or add additional context in comments.

7 Comments

haha you posted the answer, literally while i was typing it. :D
:P The solution is very simple :V
Thanks for the answer, and i got the result Date, Name and then Address at last, why it can happen? It should be like what you said
@M.AgusTriMulyono If my answer helps you mark it correctly.
@eyllanesc the result what i got is Date, Name, then Address, why it can be happen, it should be Name, Date, then Address
|
1

Assumptions made:

  1. I assumed that you are using python3.x

You can do it as follows:

import json
a = json.dumps({"Name": "Robert", "Date" : "January 17th, 2017", "Address" : "Jakarta"})
a_dict = json.loads(a)
for key in a_dict.keys():
    print (key)

Comments

0

I think you need to use loads to create a json object. So in Python3:

import json

a = json.loads('{"Name":"Robert","Date":"January 17th, 2017","Address":"Jakarta"}')

for key, value in a.items() :
    print (key)

3 Comments

loads does not create a JSON "object". It deserializes a string in JSON format into a python object
I meant set of dicts from string
the result is Date, Name and Address, why it can be happen?
0

Below python code will get the json file as input and produce a file with labels from json at all levels.

from re import sub
from os.path import abspath, realpath, join, dirname
import json
Srcfilename = input("Enter Srcfilename: ") 
file = abspath(Srcfilename)
file_open = open(file, 'r')
file_read = file_open.read()
#print(file_read)
jdata = json.loads(file_read)

def get_keys(dl, keys_list1):
        if isinstance(dl, dict):
            for key1,info1 in dl.items():
               # print(key1)
                keys_list1.append(key1)
                #print("\nlistinfo:",info1)
                if isinstance(info1, dict) or isinstance(info1, list)  :
                        get_keys(info1, keys_list1)
        elif isinstance(dl, list):
             if isinstance(dl[0], dict):
                for key2,info2 in dl[0].items():
                  #  print(key2)
                    keys_list1.append(key2)
                   # print("\nlistinfo:",info2)
                    if isinstance(info2, dict) or isinstance(info2, list)  :
                        get_keys(info2, keys_list1)
        keys_list1 = list(dict.fromkeys(keys_list1))                
        keylist='\n'.join(map(str, keys_list1))
        
        return keylist              
      

        
keys_list1=[]
Tgtfilename = input("Enter Tgtfilename: ") 
new_file = abspath(Tgtfilename)
new_file_open = open(new_file, 'w')
content=get_keys(jdata, keys_list1)
print (keys_list1)
new_file_open.write(content)
new_file_open.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.