I am trying to extract information from a JSON file from Google Finance. The requests.get() is working but then I get stuck. I have searched quite a bit and nothing suggested seems to work. This is what I have:
import requests
from json import loads
params={'q': 'NASDAQ:AAPL','output': 'json'}
response = requests.get('https://finance.google.com/finance', params=params, allow_redirects=False, timeout=10.0)
print(response.status_code)
print(response.content)
The output is “200” which is the ok output I believe. print(response.content) gives me the full JSON string so that seems to be working ok.
However, trying to pass it into “data” so that I can work with further, extract various bits. This is what I have tried:
data = response.json() gives me JSONDecodeError: Expecting value: line 2 column 1 (char 1)
data = json.load(response) gives me AttributeError: 'Response' object has no attribute 'read'
data = json.loads(response) gives me TypeError: the JSON object must be str, bytes or bytearray, not 'Response'
I tried data = json.loads(response.decode("utf-8")) and that gives me AttributeError: 'Response' object has no attribute 'decode'. I have also tried some text scrubbing suggestions, nothing has worked yet.
I printed the text. and I found that first some data is not json string...
\n // chars...
jsonstr = response.text[4:] #remove first part (not json data)
data = loads(jsonstr)
print(data)
print("t=",data[0]['t'])
output
[{'t': 'AAPL', 'kr_annual_date': '2017', 'hi': '180.52', 'keyratios': [{'title': 'Net profit margin', 'annual': '21.09%', 'recent_quarter': '25.67%', 'ttm': '22.21%'}, {'title': 'Operating margin', 'annual': '26.76%', 'rece ....
....
com/'}]}]
t= AAPL
Try using response.content or response.text to convert to JSON.
Ex:
json.loads(response.content)
or
json.loads(response.text)
Related
Hello everyone here is a snippet of my nested json Response.
https://pastebin.com/qTk21iwX
What I want to accomplished to get all the "Row" data from this json response but I have yet to figure out the correct method of approaching this.
This is what I have tried is:
response = requests.get(url, headers=headers, json={"script": sql}, verify=verify).json()
print(response)
responseobject = json.dumps(response)
print(responseobject)
for object in responseobject['Result']['Results']:
print (object)
My output after running this code
TypeError: string indices must be integers
Any suggestions ?
responseobject = json.dumps(response)
Here you are turning the response object into a string. So when you do responseobject['Result'], you are indexing a string, not a dictionary.
To fix the problem, just remove responseobject = json.dumps(response) and do response['Result']['Results'] instead of responseobject['Result']['Results'].
I'm trying to read the response from an API, but whenever I try to access the JSON elements _ keep getting errors like:
...
...
print(parsed_data['result']['listing']['whisper'])
TypeError: list indices must be integers or slices, not str
My current code looks like:
import json
import requests
from bs4 import BeautifulSoup
item_data_api = "https://www.pathofexile.com/api/trade/fetch/e6c3075c2bea510e868dd9568930c74cfd7d926e2dc74dd12b1119ecb565b3ff"
r = requests.get(item_data_api)
if r.status_code == 200:
print(r.encoding)
parsed_data = r.json()
print(parsed_data)
print(parsed_data['result']['listing']['whisper'])
If I get the output from print and validate the JSON on https://jsonlint.com/, it is not valid:
Error: Parse error on line 1:
[{ 'id': 'e6c3075c2bea5
---^
Expecting 'STRING', '}', got 'undefined'
However, if I execute the call in a browser, the response is valid.
What am I missing here? Can someone please provide a hint on how to make the output valid? I already tried json.loads(r.text) but doesn't work either.
In this case the issues is the data you are being returned is not in the format you are expecting.
This line:
print(parsed_data['result']['listing']['whisper'])
assumes that you are getting a dictionary that looks like:
result = {
"results": {
"listing": {
"whisper": ...}}}
That's not what is being returned, it's more like:
result = {
"results": [
{"listing": ... }
{"listing": ... }]}
So results is actually returning a list of dictionaries, which is why you get the error - you can't index into a list using a string.
If I run your code, and pull out the first results dictionary I don't get your error:
print(parsed_data['result'][0]['listing']['whisper'])
I am somewhat new to working with API's and am receiving a graphconnection is not serializable error when trying to json.dumps an api response. The response looks to be a json array however, when i run Type(api_response) nothing is output.
Any help would be appreciated.
I have tried Type(api_response)
api_response = api_instance.graph_user_group_members_list(group_id, content_type, accept,limit=limit, skip=skip, x_org_id=x_org_id)
#pprint(api_response)
data = json.dumps(api_response)
I would like to convert the api_response to python dict format so that I can use the json values....
I am receiving this error
TypeError: Object of type GraphConnection is not JSON serializable
This is the output if I print api_response
[{'_from': None, 'to': {'id': '', 'type': 'user'}}, {'_from': None,
'to': {'id': '', 'type': 'user'}}]
It's likely because of the brackets around the json object. The data within the curly braces is valid json. Try to index the api_response (like jsonObject = api_response[0]) to get the 'first element' of the api response object.
I actually was able to find the solution by using ([ob.dict for ob in api_response]). Still think I am missing some knowledge surrounding how json.dumps works.
I'm trying to get a parameter from a JSON string loaded with requests.
I think I tried any combination I can think of.
Any hints are very appreciated.
My code is this:
r = requests.get(url, headers=headers)
json_string = r.json
status = json.dumps(json_string['httpStatusCode'])
and I'm getting
'method' object is not subscriptable
The error you are getting because you are assigning a "method" object to json_string.Since in python "method" objects are not subscriptable.
To get JSON response you have to do this
json_string = r.json()
I'm trying to pass a variable to the data field in requests.post() and I continue to get the error,
Error Response: {'error': {'message': 'Exception while reading request',
'detail': 'Cannod decode: java.io.StringReader#1659711'}, 'status': 'failure'}
Here is my code
#Fill array from CSV
temp=[]
for row in csv.iterrows():
index, data = row
temp.append(data.tolist())
#Create new asset for all assets in CSV
for index, row in enumerate(temp):
make = temp[index][0]
serial = str(temp[index][1])
date = str(temp[index][2])
response = requests.post(url, auth=(user, pwd), headers=headers,
data='{"asset_tag":"test1", "assigned_to":"test2",
"company":"test3", "serial_number":serial}')
I originally tried feeding it directly from the CSV using
str(temp[index][1])
This did not work, so I tried assigning str(temp[index][1]) to the variable serial and then passing the variable like that but that also results in the same error.
A point in the right direction would be great, thanks!
Instead of sending the request payload body in string, pass it in json form.
requests.post accepts string in data variable and json in json variable. I faced a same issue while trying to make my first REST call to ServiceNow instance via Python. Hope this helps.
response = requests.post(url, auth=(user, pwd), headers=headers,
json={"asset_tag":"test1", "assigned_to":"test2",
"company":"test3", "serial_number":serial})
Remove the single quotes from the following :
data='{"asset_tag":"test1", "assigned_to":"test2",
"company":"test3", "serial_number":serial}'
Use
data = {"asset_tag":"test1", "assigned_to":"test2",
"company":"test3", "serial_number":serial}
rather than passing data=data, take data as dict and pass it as json=data.