How to get specific element from a JSON file in Python? - python

I am using The Guardian's API to get JSON data and now I want a specific element from the JSON data entry. How do I do that?
I am making the following GET request
url = 'http://content.guardianapis.com/search?from-date=2016-01-02&to-date=2016-01-02&order-by=newest&show-fields=all&page-size=1&api-key=API-KEY.
And now I want webTitle from the JSON data. So I am doing as follows:
response = requests.get(url)
response = response.content
response = json.loads(response)
content = response['response']['results']['webTitle']
But I am getting TypeError: string indices must be integers, not str error.
However, content = response['response']['results']works correctly.
Here is a spanshot of JSON data.

response = request.get(url).json()
content = response['response']['results'][index]['webTitle']
Edit
Yes as Sudheesh Singanamalla suggests, results is a list and you You'd have to access a given index to get corresponding webTitle

We get this error when we mistake a list with a dictionary. Seems like 'results' is actually a list with dictionary as its first item. Do the following instead.
content = response['response']['results'][0]['webTitle']
And obviously I assumed you have one item in the 'results' list only. If it has multiple items, iterate over the list.

response['response']['results'] provides you with a list of dictionaries. [{},{},...] where each dictionary contains the following in your example:
{id:'', type:'', sectionid:'', .... }
If you want the list of all the webTitle in the results you'd have to iterate over the list obtained from response['response']['results'] and select the webTitle or any other required key in the resulting dictionary. Here's an example
webTitleList = []
# Make the request for the JSON
results = response['response']['results']
for result in results:
webTitleList.append(result['webTitle'])
# Now the webTitleList contains the list of all the webTitles in the returned json
However, you can also simplify the selection of the webTitle by using list comprehension on results. There are numerous examples on StackOverflow for the same.

results is a list. Iterate over it to fetch the required value.
Ex:
for i in response['response']['results']:
print(i['webTitle'])

Related

Looping through json.loads(response.text) with Python

i'm learning and would appreciate any help in this code.
The issue is trying to print the values in the data that are contained in one line of the JSON using Python.
import json
import requests
data = json.loads(response.text)
print(len(data)) #showing correct value
#where i'm going wrong below obviously this will print the first value then the second as it's indexed. Q how do I print all values when using seperate print statements when the total indexed value is unknown?
for item in data:
print(data[0]['full_name'])
print(data[1]['full_name'])
I tried without the index value this gave me the first value multiple times depending on the length.
I expect to be able to access from the JSON file each indexed value separately even though they are named the same thing "full_name" for example.
import json
import requests
data = json.loads(response.text)
print(len(data)) #showing correct value
for item in data:
print(item['full_name'])
#the below code will throw error.. because python index starts with 0
print(data[0]['full_name'])
print(data[1]['full_name'])
hope this help
Presuming data is a list of dictionaries, where each dictionary contains a full_name key:
for item in data:
print(item['full_name'])
This code sample from your post makes no sense:
for item in data:
print(data[0]['full_name'])
print(data[1]['full_name'])
Firstly it's a syntax error because there is nothing indented underneath the loop.
Secondly it's a logic error, because the loop variable is item but then you never refer to that variable.

What is a good way to parse JSON response with varied data types?

I am trying to write API calls to retrieve the last package status using UPS Tracking API. However, it seems that the JSON strings returned by the API vary in the data type returned - for example, ["TrackResponse"]["Shipment"]["Package"] returns either a JSON string or a list of JSON strings.
I am currently managing these using the below try / except statements. If a list of JSONs is returned instead of a singular JSON string, I specify that I am looking for the first JSON string in the list.
response = requests.request("POST", url, data=payload, headers=headers)
response_json=response.json()
try:
status = response_json["TrackResponse"]["Shipment"]["Package"]["Activity"][0]["Status"]["Type"]
code = response_json["TrackResponse"]["Shipment"]["Package"]["Activity"][0]["Status"]["Code"]
location = response_json["TrackResponse"]["Shipment"]["Package"]["Activity"][0]["ActivityLocation"]["Address"]["City"]
except:
status = response_json["TrackResponse"]["Shipment"]["Package"][0]["Activity"][0]["Status"]["Type"]
code = response_json["TrackResponse"]["Shipment"]["Package"][0]["Activity"][0]["Status"]["Code"]
location = response_json["TrackResponse"]["Shipment"]["Package"][0]["Activity"][0]["ActivityLocation"]["Address"]["City"]
Unfortunately, this seems like it isn't going to work well as there are multiple parts of the API response where this issue is occurring. Eg."Activity" also returns a JSON if only one activity was found, or a list of JSONs if multiple activities are returned.
Is there a more elegant way of resolving these issues, besides writing multiple nested if/else or try/except statements to test the datatype returned at each point of the JSON response?
Thanks!

Getting value from api dict response

Im using requests with the Bitmex API, im trying to get the lastPrice value from the get requests.
I stored the response into a variable, tried a few ways into pulling the lastPrice value including print(value[1]) print(value['lastPrice'], all of which didnt work, ive been reading for a while now on here and cant seem to find the correct working way to get the value. sorry if this is asked all the time.
import requests
r = requests.get('https://www.bitmex.com/api/v1/instrument?symbol=XBT&columns=lastPrice&count=1&reverse=true')
value = r.text
print(value)
#print(value[1])
#print(value['lastPrice'])
The output from this is
[{"symbol":"XBTUSD","timestamp":"2019-10-03T22:37:13.085Z","lastPrice":8190.5}]
using value[1] just returns the first letter in the print. So for ex [1] returns { and using ['lastPrice'] returns
TypeError: string indices must be integers
Your return value is JSON string, you can use response.json() to decode it to python dict. Result contains list with single element, so you should refer to first element of list and then get value from dict by key:
r = requests.get('https://www.bitmex.com/api/v1/instrument?symbol=XBT&columns=lastPrice&count=1&reverse=true')
value = r.json()
print(value[0]['lastPrice'])

Extract a dictionnary value in Python

I have an API call result in Python which is returning the following:
b'[{"type":"deposit","currency":"bch","amount":"0.00000001","available":"0.00000001"}]'
I tried to extract the value 0.00000001 but without any success.
I know how to extract values from lists and dictionaries in Python,but as there is the b' value before the results I am not figuring out how to get it.
Any ideas?
I think what you have here is actually a bytes string, rather than a Python dictionary. Try this to convert it to a dictionary (actually a list containing a dictionary given the square brackets):
import json
data = json.loads(b'[{"type":"deposit","currency":"bch","amount":"0.00000001","available":"0.00000001"}]')
value = data[0]['amount']
The API is probably returning json data, you should parse it this way:
import json
data = json.loads(json_data)
print data[0]['amount']
json_data is what the API returns

Appending to Requests JSON result

Using Requests to return JSON from an API.
Assigning the parts of the result I need to variables - working fine.
for i in searchResult['inventoryAsset']:
try:
assetID = i['assetID']
initialUrl = i['webSite']['initialUrl']
finalUrl = i['webSite']['finalUrl']
initialASN = i['asn']['asnID']
initialHost = i['host']['host']
existingTags = i['tags']
finalHost = urlparse(finalUrl).hostname
print existingTags
>>> [u'Some Tag', u'Some other Tag']
Now I need to append an extra value to this variable (existingTags) and resubmit to the API as JSON to update the record. My problem is I don't know 'what' this variable now is - is it a Dict, a List or still JSON?
Once I understand this I can research the right way to append a new value. I've tried treating it as a List using .extend which looks right when printed but is not accepted by the API. Don't think it's a Dict either as I'd expect to see the keys when printed.
existingTags.extend(['A New Tag'])
Produces 400 error:
[Found invalid item(s) for updating tags:[Some Tag, Some other Tag, A New Tag]]
Resubmitting without touching the existingTags variable works.

Categories