json.load()
By using file object in Python we can read Json formatted data from a file using load() method.
« Json
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()
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