Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with json data
#1
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.
Reply
#2
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.
Reply
#3
I tried as below
value3=jData['facet_ranges']['indexTimestamp']
             
print value3.counts[1]
execution of above resulted in below error
KeyError: 'facet_ranges'
Reply
#4
That's the same code, with the same error.
When in doubt, print it out! print(jData)
Reply
#5
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,
Reply
#6
(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! print(jData)

you don't listen to @nilamo. What he asks is
print jData
if 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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to get json data by request with header? dokipo 0 1,964 Nov-06-2021, 04:51 PM
Last Post: dokipo
  Load JSON Data Over Several Templates martin28 4 3,036 Jan-03-2019, 07:44 PM
Last Post: taldridge

Forum Jump:

User Panel Messages

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