Appending to Requests JSON result - python

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.

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.

Python Iterating Array of URI to retrieve JSON

I have a dynamically created array of URIs that I want to iterate through for retrieval of json data, which I'll then use to search a particular field for a specific value. Unfortunately, I keep getting syntax errors.
for i in list_url_id:{
var t = requests.get(base_url+i+'?limit='+str(count),auth=HTTPBasicAuth(uname,pw)).json()
print(t)
}
If I do a print(i) in the loop, it prints the full URL out properly. I'm lost.
EDIT:
base_url is a URL similar to https://www.abcdef.com:1443
the URI in list_url_id is a URI similar to /v1/messages/list/0293842
I have no issue (as mentioned) concatenating them into a print operation, but when used for the string for requests.get, it returns a nondescript syntax error
Python sees the code inside the bracket as a dictionary, that's probably causing the syntax errors.
Indentation is used in Python for traversing for loops.
for i in list_url_id:
t = requests.get(base_url+i+'?limit='+str(count),auth=HTTPBasicAuth(uname,pw)).json()
print(t)
This should work for you and note that var is also removed as it is the wrong syntax. Python variables do not need an explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable.

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

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

Python splitting values from urllib in string

I'm trying to get IP location and other stuff from ipinfodb.com, but I'm stuck.
I want to split all of the values into new strings that I can format how I want later. What I wrote so far is:
resp = urllib2.urlopen('http://api.ipinfodb.com/v3/ip-city/?key=mykey&ip=someip').read()
out = resp.replace(";", " ")
print out
Before I replaced the string into new one the output was:
OK;;someip;somecountry;somecountrycode;somecity;somecity;-;42.1975;23.3342;+05:00
So I made it show only
OK someip somecountry somecountrycode somecity somecity - 42.1975;23.3342 +05:00
But the problem is that this is pretty stupid, because I want to use them not in one string, but in more, because what I do now is print out and it outputs this, I want to change it like print country, print city and it outputs the country,city etc. I tried checking in their site, there's some class for that but it's for different api version so I can't use it (v2, mine is v3). Does anyone have an idea how to do that?
PS. Sorry if the answer is obvious or I'm mistaken, I'm new with Python :s
You need to split the resp text by ;:
out = resp.split(';')
Now out is a list of values instead, use indexes to access various items:
print 'Country: {}'.format(out[3])
Alternatively, add format=json to your query string and receive a JSON response from that API:
import json
resp = urllib2.urlopen('http://api.ipinfodb.com/v3/ip-city/?format=json&key=mykey&ip=someip')
data = json.load(resp)
print data['countryName']

Categories