Felix B
Felix B

Reputation: 157

How to manipulate json file in Python

I want to pull data from here: http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getMakes

Here's my python script so far:

import urllib.request
import json
url =("http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getMakes")
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
result = response.read().decode('utf-8')
print (result)

When I try to manipulate the resultes, starting with:

d = json.loads(result)

I get an error message: "ValueError: No JSON object could be decoded"

What am I doing wrong?

Upvotes: 0

Views: 284

Answers (2)

emvee
emvee

Reputation: 4449

Your URL/query is wrong; you are not getting a valid JSON reply back from the server; it starts like this:

?({"Makes":[{"make_id":"abarth","make_display":"Abarth","make_is_common":"0","make_country":"Italy"},

If you modify url like this:

url = ("http://www.carqueryapi.com/api/0.3/?cmd=getMakes")

it should work. At least it worked for me after that.

Apparently, the callback=? is meant such that the client can insert a callback method which gets passed the json. I.e. you can have the server generate executable javascript rather than just json.

Upvotes: 2

TheGreatContini
TheGreatContini

Reputation: 6639

If I click your URL, the first characters in the body are "?(". That's corrupting your attempt to feed it into JSON. You will need to pre-process the data to strip out the junk. There is also junk at the end.

Save yourself headaches by using Python requests library rather than urllib, by the way!

Upvotes: 0

Related Questions