python how to create json file code example

Example 1: json dump to file

import json

data = {"key": "value"}

with open('data.json', 'w') as jsonfile:
    json.dump(data, jsonfile)

Example 2: get json from file python

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('')

Example 3: write json pythonb

>>> jstr = json.dumps(data, ensure_ascii=False, indent=4)
>>> print(jstr)
{
    "item": "Beer",
    "cost": "£4.00"
}