# Python program to write JSON
# to a file
import json
# Data to be written
dictionary ={
"name" : "sathiyajith",
"rollno" : 56,
"cgpa" : 8.6,
"phonenumber" : "9976770500"
}
with open("sample.json", "w") as outfile:
json.dump(dictionary, outfile)
# Directly from dictionary
with open('json_data.json', 'w') as outfile:
json.dump(json_string, outfile)
# Using a JSON string
with open('json_data.json', 'w') as outfile:
outfile.write(json_string)
On a modern system (i.e. Python 3 and UTF-8 support), you can write a nice file with:
import json
with open('data.json', 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
# Directly from dictionary
with open('json_data.json', 'w') as outfile:
json.dump(json_string, outfile)
# Using a JSON string
with open('json_data.json', 'w') as outfile:
outfile.write(json_string)
# Directly from dictionary
with open('json_data.json', 'w') as outfile:
json.dump(json_string, outfile)
# Using a JSON string
with open('json_data.json', 'w') as outfile:
outfile.write(json_string)
import json
with open('data.txt') as json_file:
data = json.load(json_file)
for p in data['people']:
print('Name: ' + p['name'])
print('Website: ' + p['website'])
print('From: ' + p['from'])
print('')
# Directly from dictionary
with open('json_data.json', 'w') as outfile:
json.dump(json_string, outfile)
# Using a JSON string
with open('json_data.json', 'w') as outfile:
outfile.write(json_string)
# Directly from dictionary
with open('json_data.json', 'w') as outfile:
json.dump(json_string, outfile)
# Using a JSON string
with open('json_data.json', 'w') as outfile:
outfile.write(json_string)
# Directly from dictionary
with open('json_data.json', 'w') as outfile:
json.dump(json_string, outfile)
# Using a JSON string
with open('json_data.json', 'w') as outfile:
outfile.write(json_string)
# Directly from dictionary
with open('json_data.json', 'w') as outfile:
json.dump(json_string, outfile)
# Using a JSON string
with open('json_data.json', 'w') as outfile:
outfile.write(json_string)
# Directly from dictionary
with open('json_data.json', 'w') as outfile:
json.dump(json_string, outfile)
# Using a JSON string
with open('json_data.json', 'w') as outfile:
outfile.write(json_string)