python - Trying to print JSON value -
i have json output dell website , i'm trying access values having hard time accessing values.
for example, have following json being retrieved:
{ "getassetwarrantyresponse": { "getassetwarrantyresult": { "faults": null, "response": { "dellasset": { "machinedescription": "t350, new balance", "shipdate": "2010-05-04", "servicetag": "89m1", "ordernumber": 518, "localchannel": 25, "assetparts": { "@nil": "true" }, "countrylookupcode": 11, "itemclasscode": "v02", "isduplicate": "false", "parentservicetag": { "@nil": "true" }, "customernumber": 1121, "warranties": { "warranty": [ { "startdate": "2011-05-05", "enddate": "2013-05-04", "serviceprovider": "u", "servicelevelcode": "d", "itemnumber": "992-82", "entitlementtype": "extended", "serviceleveldescription": "next business day support", "servicelevelgroup": 5 }, { "startdate": "2010-05-04", "enddate": "2011-05-04", "serviceprovider": "u", "servicelevelcode": "n", "itemnumber": "993-0", "entitlementtype": "initial", "serviceleveldescription": "next business day support", "servicelevelgroup": 5 } ] } } } } } }
so example, if wanted retrieve value of "servicetag" how go getting in python? reason when try things like:
raw_data = urllib2.urlopen('https://json.url') djson = raw_data.read() djdata = (json.dumps(json.loads(djson), indent=4)) print djdata['getassetwarrantyresponse']['getassetwarrantyresult']['faults']['response']['dellasset'][0]['servicetag']
i'm getting:
traceback (most recent call last): file "./warranty.py", line 35, in <module> print djdata['getassetwarrantyresponse']['getassetwarrantyresult']['faults']['response']['dellasset'][0]['servicetag'] typeerror: string indices must integers, not str
use json.loads
create python dict containing json information.
then, access python dict.
import json mydict = json.loads(json_response) print mydict['getassetwarrantyresponse']['getassetwarrantyresult']['response']['dellasset']['servicetag']
Comments
Post a Comment