Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python3: Read json file from url

Tags:

python

json

url

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
like image 285
Ivan Castro Avatar asked Sep 29 '16 21:09

Ivan Castro


People also ask

How do I read a JSON file in Python?

json.loads(): If you have a JSON string, you can parse it by using the json.loads() method.json.loads() does not take the file path, but the file contents as a string, using fileobject.read() with json.loads() we can return the content of the file. Example: This example shows reading from both string and JSON file.

Can we read JSON data directly from a Web service via HTTP?

JSON Web Services let you access portal service methods by exposing them as a JSON HTTP API. Service methods are made easily accessible using HTTP requests, both from JavaScript within the portal and from any JSON-speaking client.

How do you read a JSON response in Python?

Just execute response. json() , and that's it. response. json() returns a JSON response in Python dictionary format so we can access JSON using key-value pairs.


3 Answers

You were close:

import requests
import json
response = json.loads(requests.get("your_url").text)
like image 70
coder Avatar answered Oct 23 '22 01:10

coder


Just use json and requests modules:

import requests, json

content = requests.get("http://example.com")
json = json.loads(content.content)
like image 45
Juan Albarracín Avatar answered Oct 23 '22 02:10

Juan Albarracín


Or using the standard library:

from urllib.request import urlopen
import json

data = json.loads(urlopen(url).read().decode("utf-8"))
like image 5
automatist Avatar answered Oct 23 '22 01:10

automatist