json.dump()

By using file object in Python we can write Json formatted data to a file using dump() method.

Using List
import json
my_list=['Alex','Ronald','Ronny']

fob=open('data_j3.txt','w')
json.dump(my_list, fob)
fob.close()
Using Dictionary
import json
my_dict={1:'Alex',2:'Ronald',3:'Ronny'}
with open("data_j2.json", "w") as fob:
    json.dump(my_dict, fob)
fob.close()
import json
my_dict={1:'Alex',2:'Ronald'}

fob=open('data_j.txt','w')
json.dump(my_dict, fob)
fob.close()

From URL to Json file

import json
from urllib.request import urlopen
url="https://www.plus2net.com/php_tutorial/student.json"  
f=urlopen(url)   # open 
data=json.load(f) # data collected 
fob=open('E:\\my_data\\data_j.json','w')
json.dump(data, fob)
fob.close()
All above scripts will create text files and these files can be checked for Json data format. Here is one sample code to read the data from a file.
fob=open('data_j2.json','r')
print(fob.read()) # {"1": "Alex", "2": "Ronald", "3": "Ronny"}
fob.close()
ensure_ascii Parameter: Use ensure_ascii=False to handle non-ASCII characters in the output.
import json
data = {"name": "José", "country": "España"}
with open("E:\\data.json", "w", encoding="utf-8") as file:
    json.dump(data, file, ensure_ascii=False)

load() to read Json data from file
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer