How to get JSON from webpage into Python script

Each Answer to this Q is separated by one/two green lines.

Got the following code in one of my scripts:

# url is defined above.
jsonurl = urlopen(url)

# While trying to debug, I put this in:
print jsonurl

# Was hoping text would contain the actual json crap from the URL, but seems not...
text = json.loads(jsonurl)
print text

What I want to do is get the {{.....etc.....}} stuff that I see on the URL when I load it in Firefox into my script so I can parse a value out of it. I’ve Googled a ton but I haven’t found a good answer as to how to actually get the {{...}} stuff from a URL ending in .json into an object in a Python script.

Get data from the URL and then call json.loads e.g.

Python3 example:

import urllib.request, json 
with urllib.request.urlopen("") as url:
    data = json.loads(

Python2 example:

import urllib, json
url = ""
response = urllib.urlopen(url)
data = json.loads(
print data

The output would result in something like this:

"results" : [
    "address_components" : [
            "long_name" : "Charleston and Huff",
            "short_name" : "Charleston and Huff",
            "types" : [ "establishment", "point_of_interest" ]
            "long_name" : "Mountain View",
            "short_name" : "Mountain View",
            "types" : [ "locality", "political" ]

I’ll take a guess that you actually want to get data from the URL:

jsonurl = urlopen(url)
text = json.loads( # <-- read from it

Or, check out JSON decoder in the requests library.

import requests
r = requests.get('someurl')
print r.json() # if response type was set to JSON, then you'll automatically have a JSON response here...

This gets a dictionary in JSON format from a webpage with Python 2.X and Python 3.X:

#!/usr/bin/env python

    # For Python 3.0 and later
    from urllib.request import urlopen
except ImportError:
    # Fall back to Python 2's urllib2
    from urllib2 import urlopen

import json

def get_jsonparsed_data(url):
    Receive the content of ``url``, parse it as JSON and return the object.

    url : str

    response = urlopen(url)
    data ="utf-8")
    return json.loads(data)

url = (""

See also: Read and write example for JSON

I have found this to be the easiest and most efficient way to get JSON from a webpage when using Python 3:

import json,urllib.request
data = urllib.request.urlopen("").read()
output = json.loads(data)
print (output)

you need import requests and use from json() method :

source = requests.get("url").json()

Of course, this method also works:

import json,urllib.request
data = urllib.request.urlopen("url").read()
output = json.loads(data)
print (output)

json.loads will decode it into a Python object using this table, for example a JSON object will become a Python dict.

All that the call to urlopen() does (according to the docs) is return a file-like object. Once you have that, you need to call its read() method to actually pull the JSON data across the network.

Something like:

jsonurl = urlopen(url)

text = json.loads(
print text

In Python 2, json.load() will work instead of json.loads()

import json
import urllib

output = json.load(urllib.urlopen(url))

Unfortunately, that doesn’t work in Python 3. json.load is just a wrapper around json.loads that calls read() for a file-like object. json.loads requires a string object and the output of urllib.urlopen(url).read() is a bytes object. So one has to get the file encoding in order to make it work in Python 3.

In this example we query the headers for the encoding and fall back to utf-8 if we don’t get one. The headers object is different between Python 2 and 3 so it has to be done different ways. Using requests would avoid all this, but sometimes you need to stick to the standard library.

import json
from six.moves.urllib.request import urlopen

urlResponse = urlopen(url)

if hasattr(urlResponse.headers, 'get_content_charset'):
    encoding = urlResponse.headers.get_content_charset(DEFAULT_ENCODING)
    encoding = urlResponse.headers.getparam('charset') or DEFAULT_ENCODING

output = json.loads(

For python>=3.6 you can use:

import dload

j = dload.json(url)

Install dload with:

pip3 install dload

There’s no need to use an extra library to parse the json…

json.loads() returns a dictionary.

So in your case, just do text["someValueKey"]

Not sure why all the earlier answers are using json.loads. All you need is:

import json
from urllib.request import urlopen

f = urlopen("")
j = json.load(f)

This works because urlopen returns a file-like object, which works with json.load.

you can use json.dumps:

import json

# Hier comes you received data

data = json.dumps(response)


for loading json and write it on file the following code is useful:

data = json.loads(json.dumps(Response, sort_keys=False, indent=4))
with open('data.json', 'w') as outfile:
json.dump(data, outfile, sort_keys=False, indent=4)

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.