json.dump python code example

Example 1: python json dump to file

import json
with open('data.json', 'w') as f:
    json.dump(data, f)

Example 2: python json dump

import json

with open("data_file.json", "w") as write_file:
    json.dump(data, write_file)

Example 3: json load

import json

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

Example 4: python to json

# a Python object (dict):
x = {
  "name": "John",
  "age": 30,
  "city": "New York"
}

# convert into JSON:
y = json.dumps(x)

Example 5: 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'''