Answers for "python print json object"

66

python read json file

import json

with open('path_to_file/person.json') as f:
  data = json.load(f)

print(data)
Posted by: Guest on April-08-2020
8

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))
Posted by: Guest on May-14-2020
2

python write to json with indent

import json

# alphabetic dictionary
json_data = {hex(k): chr(k) for k in range(ord('a'), ord('z') + 1)}
# to file
with open("my_json.json", "w") as file:
    json.dump(json_data, file, indent=4, sort_keys=True)
Posted by: Guest on June-29-2020

Python Answers by Framework

Browse Popular Code Answers by Language