![]() |
Issue with json data - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Issue with json data (/thread-17277.html) |
Issue with json data - pythonFresher - Apr-04-2019 Hi All, I am new to python and please excuse for any process in posting the thread.I have a requirement to get few values from a URL. URL http://xx.xx.xx.net:xx/xx/xx/select?=&facet.mincount=1&facet.pivot={!range=d1}merchant&facet.range.end=NOW&facet.range.gap=%2B1DAY&facet.range.start=NOW/DAY-90DAYS&facet.range={!tag=d1}indexTimestamp&facet=true&indent=on&q=*:*&rows=0&wt=json Part of the result of the above URL: "facet_ranges": { "indexTimestamp": { "counts": [ "2019-01-23T00:00:00Z", 3804, "2019-03-21T00:00:00Z", 436101, "2019-03-22T00:00:00Z", 11824, "2019-03-23T00:00:00Z", 1170, "2019-03-24T00:00:00Z", 744, "2019-03-25T00:00:00Z", 905, "2019-03-26T00:00:00Z", 1116, "2019-03-27T00:00:00Z", 1254, "2019-03-28T00:00:00Z", 1324, "2019-03-29T00:00:00Z", 1371, "2019-03-30T00:00:00Z", 1603, "2019-03-31T00:00:00Z", 1069, "2019-04-01T00:00:00Z", 1164, "2019-04-02T00:00:00Z", 4278, "2019-04-03T00:00:00Z", 1621986, "2019-04-04T00:00:00Z", 1223949 ], "gap": "+1DAY", "start": "2019-01-04T00:00:00Z", "end": "2019-04-05T00:00:00Z" } }, From the above output my requirement is to get each days count, atleast last 2 days.ie 2019-04-04T00:00:00Z : 1223949 2019-04-03T00:00:00Z : 1621986 2019-04-02T00:00:00Z :4278 responseurl = requests.get('http://xx.xx.xx.net:xx/xx/xx/select?=&facet.mincount=1&facet.pivot={!range=d1}merchant&facet.range.end=NOW&facet.range.gap=%2B1DAY&facet.range.start=NOW/DAY-90DAYS&facet.range={!tag=d1}indexTimestamp&facet=true&indent=on&q=*:*&rows=0&wt=json') if(responseurl.ok): jData = json.loads(responseurl.content) value2=jData['facet_ranges']['indexTimestamp']['counts'] print value2 else: responseurl.raise_for_status()Executing the above code resulted in below error. value2=jData['facet_ranges']['indexTimestamp']['counts'] KeyError: 'facet_ranges' I am unable to figure out the issue,could please help. RE: Issue with json data - nilamo - Apr-04-2019 When in doubt, print it out! The json parsing isn't failing, so it's valid json. But it obviously doesn't look like you think it does. RE: Issue with json data - pythonFresher - Apr-05-2019 I tried as below value3=jData['facet_ranges']['indexTimestamp'] print value3.counts[1]execution of above resulted in below error KeyError: 'facet_ranges' RE: Issue with json data - nilamo - Apr-05-2019 That's the same code, with the same error. When in doubt, print it out! print(jData)
RE: Issue with json data - pythonFresher - Apr-07-2019 have researched but couldn't get the piece of information. any hint how it can be done will be highly appreciated as there is some deadline for this work, RE: Issue with json data - buran - Apr-07-2019 (Apr-07-2019, 05:18 AM)pythonFresher Wrote: have researched but couldn't get the piece of information. any hint how it can be done will be highly appreciated as there is some deadline for this work, (Apr-05-2019, 03:57 PM)nilamo Wrote: That's the same code, with the same error. When in doubt, print it out! you don't listen to @nilamo. What he asks is print jDataif you do, you will see, it's a dict Two additional things: 1. don't use python2, use python3 2. requests.Resonse has json() method for convenience. use it |