how to use json.dumps in python code example
Example 1: json dumps python
# How to encode JSON as a string
import json
print(json.dumps({'a': 1, 'b': 2}) # '{ "a": 1, "b": 2 }'
print(json.dumps({'b': 1, 'a': 2}, sort_keys=True, indent=4))
# {
# "a": 2,
# "b": 1
# }
Example 2: python json.dumps pretty print
json.dumps(x, indent=4)
Example 3: json load
import json
with open('path_to_file/person.json') as f:
data = json.load(f)
# Output: {'name': 'Bob', 'languages': ['English', 'Fench']}
print(data)
Example 4: json.dumps python
example={
"Playlists": [
"Default"
],
"Default": [
"Resources\\Media\\C.mp3",
"Resources\\Media\\K.mp3"
]
}
import json
json_file_path=input('Enter the path: ')
with open(json_file_path,'w') as hand:
json.dumps(example,hand,indent=4)
'''# dict,file_pointer,indentation'''