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
  I'm assuming you're asking about accessing nested dictionary keys and values in Pytho DavidSah 0 207 Dec-18-2025, 01:54 AM
Last Post: DavidSah
Question [SOLVED] Access keys and values from nested dictionary? Winfried 4 824 Nov-17-2025, 11:47 AM
Last Post: Winfried
  python env with dictionary maiya 2 2,567 Mar-22-2025, 02:07 PM
Last Post: EmilyJohnson
  Replace values in Yaml file with value in dictionary PelleH 1 3,690 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  Building a dictionary .update entry from variables Curbie 5 2,450 Sep-03-2024, 07:31 PM
Last Post: Curbie
  Dictionary using path. Bobbee 5 1,824 Aug-22-2024, 08:43 PM
Last Post: Bobbee
  Modifying a dictionary recursively SpongeB0B 2 1,866 May-12-2024, 04:09 PM
Last Post: Gribouillis
  Sort a list of dictionaries by the only dictionary key Calab 2 2,235 Apr-29-2024, 04:38 PM
Last Post: Calab
Question Using Lists as Dictionary Values bfallert 8 3,889 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  Matching Data - Help - Dictionary manuel174102 1 1,567 Feb-02-2024, 04:47 PM
Last Post: deanhystad

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.