1
2import json
3
4json_data = '[{"ID":10,"Name":"Pankaj","Role":"CEO"},' \
5 '{"ID":20,"Name":"David Lee","Role":"Editor"}]'
6
7json_object = json.loads(json_data)
8
9json_formatted_str = json.dumps(json_object, indent=2)
10
11print(json_formatted_str)
12
1import json
2
3uglyjson = '{"firstnam":"James","surname":"Bond","mobile":["007-700-007","001-007-007-0007"]}'
4
5#json.load method converts JSON string to Python Object
6parsed = json.loads(uglyjson)
7
8print(json.dumps(parsed, indent=2, sort_keys=True))
1import json
2
3# alphabetic dictionary
4json_data = {hex(k): chr(k) for k in range(ord('a'), ord('z') + 1)}
5# to file
6with open("my_json.json", "w") as file:
7 json.dump(json_data, file, indent=4, sort_keys=True)
8
1import json
2
3x = '{ "name":"John", "age":30, "city":"New York"}'
4y = json.loads(x)
5
6print(y["age"])
1ObjectMapper mapper = new ObjectMapper();
2String perttyStr = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
3