1# How to encode JSON as a string
2import json
3print(json.dumps({'a': 1, 'b': 2}) # '{ "a": 1, "b": 2 }'
4print(json.dumps({'b': 1, 'a': 2}, sort_keys=True, indent=4))
5# {
6# "a": 2,
7# "b": 1
8# }
1import json
2
3with open("data_file.json", "w") as write_file:
4 json.dump(data, write_file)
5
1>>> import json
2>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
3['foo', {'bar': ['baz', None, 1.0, 2]}]
4>>> json.loads('"\\"foo\\bar"')
5'"foo\x08ar'
6>>> from io import StringIO
7>>> io = StringIO('["streaming API"]')
8>>> json.load(io)
9['streaming API']
1
2example={
3 "Playlists": [
4 "Default"
5 ],
6 "Default": [
7 "Resources\\Media\\C.mp3",
8 "Resources\\Media\\K.mp3"
9 ]
10}
11import json
12json_file_path=input('Enter the path: ')
13with open(json_file_path,'w') as hand:
14 json.dumps(example,hand,indent=4)
15'''# dict,file_pointer,indentation'''
16