How to grab values from key:value pairs in parsed JSON in Python -


i trying extract data json file. here code.

import json json_file = open('test.json')                                                                                                                           data = json.load(json_file) json_file.close() 

at point can print file using print data , appears file has been read in properly.

now grab value key:value pair in dictionary nested within list nested within dictionary (phew!). initial strategy create dictionary , plop dictionary nested within list in , extract key:value pairs need.

dict = {} dict = json_file.get('dataobjects', none) dict[0] 

when try @ contents of dict, see there 1 element. entire dictionary appears have been read list. i've tried couple of different approaches, still wind dictionary read list. love grab nested dictionary , use .get grab values need. here sample of json working with. specifically, trying pull out identifier , description values dataobjects section.

{   "identifier": 213726,   "scientificname": "carcharodon carcharias (linnaeus, 1758)",   "richness_score": 89.6095,   "synonyms": [    ],    "taxonconcepts": [     {       "identifier": 20728481,       "scientificname": "carcharodon carcharias (linnaeus, 1758)",       "nameaccordingto": "worms species information (marine species)",       "canonicalform": "carcharodon carcharias",       "sourceidentfier": "105838"     },     {       "identifier": 24922984,       "scientificname": "carcharodon carcharias",       "nameaccordingto": "iucn red list (species assessed global conservation)",       "canonicalform": "carcharodon carcharias",       "sourceidentfier": "iucn-3855"     },   ],   "dataobjects": [     {       "identifier": "5e1882d822ec530069d6d29e28944369",       "dataobjectversionid": 5671572,       "datatype": "http://purl.org/dc/dcmitype/text",       "datasubtype": "",       "vettedstatus": "trusted",       "datarating": 3.0,       "subject": "http://rs.tdwg.org/ontology/voc/spminfoitems#taxonbiology",       "mimetype": "text/html",       "title": "biology",       "language": "en",       "license": "http://creativecommons.org/licenses/by-nc-sa/3.0/",       "rights": "copyright wildscreen 2003-2008",       "rightsholder": "wildscreen",       "audience": [         "general public"       ],       "source": "http://www.arkive.org/great-white-shark/carcharodon-carcharias/",       "description": "despite worldwide notoriety, little known natural ecology , behaviour of predator. these sharks solitary or occur in pairs, although apparently social animal can found in small aggregations of 10 or more, particularly around carcass (3) (6). females ovoviviparous; pups hatch eggs retained within mother's body, , gives birth live young (10). great white sharks particularly slow-growing, late maturing , long-lived, small litter size , low reproductive capacity (8). females not reproduce until reach 4.5 5 metres in length, , litter sizes range 2 ten pups (8). length of gestation not known estimated @ between 12 , 18 months, , these sharks reproduce every 2 or 3 years (8) (11). after birth, there no maternal care, , despite large size, survival of young thought low (8). great whites @ top of marine food chain, , these sharks skilled predators. feed predominately on fish consume turtles, molluscs, , crustaceans, , active hunters of small cetaceans such dolphins , porpoises, , of other marine mammals such seals , sea lions (12). using acute senses of smell, sound location , electroreception, weak , injured prey can detected great distance (7). efficient swimmers, sharks have quick turn of speed , attack rapidly before backing off whilst prey becomes weakened; seen leaping clear of water (6). great whites, unlike other fish, able maintain body temperature higher of surrounding water using heat exchange system in blood vessels (11).",       "agents": [         {           "full_name": "arkive",           "homepage": "http://www.arkive.org/",           "role": "provider"         }       ],     }   ] } 

the source provide cannot read json, there 2 comma's in there have delete (the 1 on fourth line bottom, , 1 2 lines above dataobjects. after json module parse without error:

import json  json_file = open('test.json') data = json.load(json_file) = data['dataobjects'][0] print do['identifier'] print do['description'] json_file.close() 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -