json.load()
Python Json By using file object in Python we can read Json formatted data from a file using load() method.
Getting a List
import json
fob=open('data_j3.txt','r')
print(json.load(fob)) # ['Alex', 'Ronald', 'Ronny']
fob.close()
Getting a Dictionary
import json
with open("data_j2.json", "r") as fob:
print(json.load(fob)) # {'1': 'Alex', '2': 'Ronald', '3': 'Ronny'}
fob.close()
import json
fob=open('data_j.txt','r')
print(json.load(fob)) # {'1': 'Alex', '2': 'Ronald'}
fob.close()
Sample text files
We used all the files created by using the code given at dump() tutorial . Same files can be used for above code.
fob=open('data_j2.json','r')
print(fob.read()) # {"1": "Alex", "2": "Ronald", "3": "Ronny"}
fob.close()
From URL to Json
import json
from urllib.request import urlopen
url="https://www.plus2net.com/php_tutorial/student.json" #Json file
f=urlopen(url) # open
data=json.load(f) # data collected
for row in data:
print(row)
loads() to write Json data to file
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com