21

In python3, I want to load this_file, which is a json format.

Basically, I want to do something like [pseudocode]:

>>> read_from_url = urllib.some_method_open(this_file)
>>> my_dict = json.load(read_from_url)
>>> print(my_dict['some_key'])
some value
2
  • What did you actually try? Your pseudocode seems close to something that would work... Commented Sep 29, 2016 at 21:38
  • That is the problem, would work... but so far I can't make it =/ Commented Sep 29, 2016 at 21:39

4 Answers 4

38

You were close:

import requests
import json
response = json.loads(requests.get("your_url").text)
Sign up to request clarification or add additional context in comments.

Comments

9

Just use json and requests modules:

import requests, json

content = requests.get("http://example.com")
json = json.loads(content.content)

1 Comment

I get TypeError: the JSON object must be str, not 'bytes'
6

Or using the standard library:

from urllib.request import urlopen
import json

data = json.loads(urlopen(url).read().decode("utf-8"))

Comments

4

So you want to be able to reference specific values with inputting keys? If i think i know what you want to do, this should help you get started. You will need the libraries urlllib2, json, and bs4. just pip install them its easy.

import urllib2
import json
from bs4 import BeautifulSoup

url = urllib2.urlopen("https://www.govtrack.us/data/congress/113/votes/2013/s11/data.json")
content = url.read()
soup = BeautifulSoup(content, "html.parser")
newDictionary=json.loads(str(soup))

I used a commonly used url to practice with.

4 Comments

urllib2 is not available in python3. And preferably I want to use only built-in libraries
Ah, well urllib should probably do the same thing? try it out.
Nice. Just change urllib2.urlopen by urllib.request.urlopen. Thanks.
Not a problem! Glad it worked, requests is a nice library too (see answers below) some great functionality in that library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.