sat1017
sat1017

Reputation: 363

Object has no attribute JSON

I am trying to print the output of an API into a local json file so I can search through the file.

I've tried looking at the type of symbols to see what the object type is but haven't had any luck. In statically typed languages I have an easier time troubleshooting due to knowing explicitly what the type is. Are there good ways to troubleshoot this type of problem moving forward?

symbols = urllib.request.urlopen("https://cloud.iexapis.com/stable/ref-data/symbols?format=json&token={}".format(key))

symbols.json.loads()
with open('data.json', 'w', encoding='utf-8') as f:
    json.dump(symbols, f, ensure_ascii=False, indent=4)

print(type(symbols))

The error I get is:

AttributeError: 'HTTPResponse' object has no attribute 'json'

Upvotes: 0

Views: 10804

Answers (2)

Roushan
Roushan

Reputation: 4440

# symbols.json.loads() #-> it is httpresponse no attribute json
json_data = json.load(symbols)
with open('data.json', 'w', encoding='utf-8') as f:
    json.dump(symbols, f, ensure_ascii=False, indent=4)

Upvotes: 0

Abhishek-Saini
Abhishek-Saini

Reputation: 743

It's because Json is a library/package in python.

symbols.json.loads() # wrong statement

In order to work with json you have to import json packagae first in your file

import json
json.loads(symbols); # you have use statement like this.

You have to pass the variable or value to the loads() function.

Upvotes: 1

Related Questions