how to use JSON in python code example

Example 1: print json python

import json

uglyjson = '{"firstnam":"James","surname":"Bond","mobile":["007-700-007","001-007-007-0007"]}'

#json.load method converts JSON string to Python Object
parsed = json.loads(uglyjson)

print(json.dumps(parsed, indent=2, sort_keys=True))

Example 2: load json

import json

with open('data.txt') as json_file:
    data = json.load(json_file)

Example 3: write json pythonb

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 4: python json file operations

import json # Imports the json file that we need
with open("jsonfile.json", "r") as jsonFile: # Opens Json file for reading
	data = json.load(jsonFile) # Reads Json file using json.load() method
'''
Example Json File:
[{"name": "Bill", "age": 20},
{"name": "Mary", "age": 31},
{"name": "Jake", "age": 19}]
'''
data[1]["age"] = 32 # Alters data variable
''' 
Now the data variable is:
[{"name": "Bill", "age": 20},
{"name": "Mary", "age": 32},
{"name": "Jake", "age": 19}]
'''
with open("jsonfile.json", "w") as fileJson: # Opens Json file once again
  json.dump(data, fileJson) # Replaces the Json file using json.dump() method

Example 5: json python

import json

# some JSON:
x = '{ "name":"John", "age":30, "city":"New York"}'

# parse x:
y = json.loads(x)

# the result is a Python dictionary:
print(y["age"])

Example 6: Json in python

import json

json_file = json.load(open("your file.json", "r", encoding="utf-8"))

# For see if you don't have error:
print(json_file)