Getting value from api dict response - python

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'])

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.

Searching JSON file for string - Python

I'm trying to learn python and get stuck on following:
This is REST API output (i attached client:AP)
I run multiple requests queries and getting different clients and CC-XXX values
data=response.content + " client:AP"
print data
{"id":"21984","key":"CC-2891","self":"https://jira.company.com/rest/api/latest/issue/21984"} client:AP
I need to extract key values (CC-2891) and client AP (sometimes client is not AP) into 2 different variables
Desired output CC-281 and client AP
import re
result = re.findall(r'\bkey:(\S+)\s, data)
print result
but getting nothing
tried also
print data['key']
but got:
TypeError: string indices must be integers, not str
You are getting a json response. Use the json module.
Ex:
import json
data = """{"id":"21984","key":"CC-2891","self":"https://jira.company.com/rest/api/latest/issue/21984"}"""
data = json.loads(data)
print(data["key"])
Output:
CC-2891
If you are using the requests module then you can directly use response.json()
Ex:
data=response.json()
print(data["key"])
Your error,
TypeError: string indices must be integers, not str
is because you have appended " client:AP" to the response.
If you want pairs of clients and the corresponding key, maybe store these yourself in a dictionary?
Like
lookup = {}
lookup["AP"] = response.json()["key"]
Then get the next response, and store that against something other than "AP".

How to get specific element from a JSON file in 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'])

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