Python Forum
Parsing json - dictionary with in dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parsing json - dictionary with in dictionary
#1
Hello I want to extract the value of all pr1 from location1 & 2 from below json - Any ideas would be appreciated
import json

jsonget = {
   "data":[  
      {  
         "location1":[  
            {  
               "pr1":"hn1",
               "pr2":"2019-08-07"
            }
         ]
      },
      {  
         "location2":[  
            {  
               "pr1":"hn2",
               "pr2":"2019-08-07"
            }
         ]
      }
   ]
}

if __name__ == '__main__':
   jsonout = json.dumps(jsonget)
   jsonout1 = json.loads(jsonout)
   for val in jsonout1['data']:
      print val['location1']

Error:
==ERROR: [{u'pr1'Traceback (most recent call last): print val['location1'] KeyError: 'location1' : u'hn1', u'pr2': u'2019-08-07'}] [Finished in 0.1s with exit code 1]
Reply
#2
The second item in 'data' has the key 'location2', but not the key 'location1'.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Can you help with a snippet to extract pr1 for each location ..

Here is how output should like

location1 hn1
location2 hn2.

Thank you.
Reply
#4
I would loop through the sub-dictionaries (for sub_dict in val.values()), and if the key you are looking for is in there, you can extract it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Jul-29-2019, 08:53 PM)krish216 Wrote: Can you help with a snippet to extract pr1 for each location ..

Here is how output should like

location1 hn1
location2 hn2.

Thank you.

import json

jsonget = {
   "data":[  
      {  
         "location1":[  
            {  
               "pr1":"hn1",
               "pr2":"2019-08-07"
            }
         ]
      },
      {  
         "location2":[  
            {  
               "pr1":"hn2",
               "pr2":"2019-08-07"
            }
         ]
      }
   ]
}
 
if __name__ == '__main__':
   jsonout = json.dumps(jsonget)
   jsonout1 = json.loads(jsonout)

   for val in jsonout1['data']:
    key = val.keys()[0]
    print key, val[key][0]["pr1"]
Reply
#6
Thanks @cvsae for the quick help.
Reply
#7
(Jul-30-2019, 09:26 PM)krish216 Wrote: Thanks @cvsae for the quick help.
you're welcome
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Matching Data - Help - Dictionary manuel174102 1 354 Feb-02-2024, 04:47 PM
Last Post: deanhystad
  Parsing large JSON josvink66 5 557 Jan-10-2024, 05:46 PM
Last Post: snippsat
  Dictionary in a list bashage 2 494 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 597 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  need to compare 2 values in a nested dictionary jss 2 797 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Sort a list of dictionaries by the only dictionary key Calab 1 452 Oct-27-2023, 03:03 PM
Last Post: buran
  python dictionary is that a bug ? rmangla 2 547 Sep-27-2023, 05:52 AM
Last Post: DPaul
  python dictionary syntax nafshar 2 840 Apr-24-2023, 07:26 PM
Last Post: snippsat
  Printing specific values out from a dictionary mcoliver88 6 1,317 Apr-12-2023, 08:10 PM
Last Post: deanhystad
  How to add list to dictionary? Kull_Khan 3 951 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020