Q:

dump method in python for json


#NOTES{The dump() method is used when the Python objects have to be stored in a file}


import json 
  
# python object(dictionary) to be dumped 
dict1 ={ 
    "emp1": { 
        "name": "Lisa", 
        "designation": "programmer", 
        "age": "34", 
        "salary": "54000"
    }, 
    "emp2": { 
        "name": "Elis", 
        "designation": "Trainee", 
        "age": "24", 
        "salary": "40000"
    }, 
} 
  
# the json file where the output must be stored 
out_file = open("myfile.json", "w") 
  
json.dump(dict1, out_file, indent = 6) 
  
out_file.close() 
1
import json

with open("data_file.json", "w") as write_file:
    json.dump(data, write_file)
0
# dictionary to be dumped 
d ={'lang':'??? ????'} 
  
with open('myfile.json', 'w', encoding ='utf8') as json_file: 
    json.dump(d, json_file, ensure_ascii = False) 
0

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'''
    
0

New to Communities?

Join the community