How to get the second level key's of JSON using python?

11,705

Solution 1

if

import json

a={ "GlobalElements" :[ { "uid":"userEmail", "passid":"userPwd", "signbuttonid": "//button[@class='btn btn-mammoth']", "logoIcon":"//a[@class='logo text-hide']", "cornerSettingMenu":"//div[@class='dropdown-toggle']/p", "logoutButtonId":"//a[@class='logout']", "overlayId":"//div[@class='overlay']", "loaderInFunctionalPanel":"//div[@class='small-inline-loader']/child::i[@class='fa fa-spinner fa-pulse']" } ]}

a= json.dumps(a) #serialize dictionary to json

b=json.loads(a)  #unserialize json to get dictionary

get the list of keys with :

l= b["GlobalElements"][0].keys()
print l

[u'uid', u'logoutButtonId', u'logoIcon', u'signbuttonid', u'passid', u'overlayId', u'loaderInFunctionalPanel', u'cornerSettingMenu']

Solution 2

a={ "GlobalElements" :[ { "uid":"userEmail", "passid":"userPwd", "signbuttonid": "//button[@class='btn btn-mammoth']", "logoIcon":"//a[@class='logo text-hide']", "cornerSettingMenu":"//div[@class='dropdown-toggle']/p", "logoutButtonId":"//a[@class='logout']", "overlayId":"//div[@class='overlay']", "loaderInFunctionalPanel":"//div[@class='small-inline-loader']/child::i[@class='fa fa-spinner fa-pulse']" } ]}

l= a["GlobalElements"][0].keys()
print l[0]
print l[1]
print l[2] so on or using for loop worked for me.

Thank you.

Solution 3

First you need to import json store your json content in a variable. if it is in a file, read the file and store it in variable. Use dumps() and loads() method to serialize and unserialize and just take the values as below code

import json

x={ "GlobalElements" :[ { "uid":"userEmail", "passid":"userPwd", "signbuttonid": "//button[@class='some id']", "logoIcon":"//a[@class='some id']", "cornerSettingMenu":"//div[@class='dropdown-toggle']/p", "logoutButtonId":"//a[@class='logout']", "overlayId":"//div[@class='overlay']", "loaderInFunctionalPanel":"//div[@class='small-inline-loader']/child::i[@class='fa fa-spinner fa-pulse']" }  ] }

x= json.dumps(x)

y=json.loads(y)
z= b["GlobalElements"][0].keys()

here z will be holding the first value of the second level.

Share:
11,705

Related videos on Youtube

Deepak N
Author by

Deepak N

Working as a developer in merahkee tech solutions.

Updated on June 04, 2022

Comments

  • Deepak N
    Deepak N almost 2 years

    Below is content of JSON file, how can I get only the keys of the second level, that means I should be able to store the keys like uid,passid,signbuttonid,logoIcon,cornerSettingMenu,logoutButtonId,overlayId,loaderInFunctionalPanel this keys I should be able to store in a list or some array using python. means I need like

    list[0]=  uid
    
    list[1]=passid
    
    list[2]=  signbuttonid
    
    list[3]=logoIcon and so on . . . . . . 
    

    { "GlobalElements" : [ { "uid":"userEmail", "passid":"userPwd", "signbuttonid": "//button[@class='btn btn-mammoth']", "logoIcon":"//a[@class='logo text-hide']", "cornerSettingMenu":"//div[@class='dropdown-toggle']/p", "logoutButtonId":"//a[@class='logout']", "overlayId":"//div[@class='overlay']", "loaderInFunctionalPanel":"//div[@class='small-inline-loader']/child::i[@class='fa fa-spinner fa-pulse']" } ]

    Note: I don't need values of that. I need only keys.

    Can Anybody help me on this. Thanks in advance.

    • Bart
      Bart about 6 years
      Just get the keys from the second level dictionary with .keys() python dictionary docs
    • Deepak N
      Deepak N about 6 years
      But it is giving whole uid,passid, singbuttonid so on, what I have to do if I need get only uid??
    • Bart
      Bart about 6 years
      It gives you a list with the keys, like you wanted in your question
  • Deepak N
    Deepak N about 6 years
    Yes, this Helped me and using index like l[0],l[1] helped me, Thank you.