Nobu
0
Q:

python json dump


  import json

# some JSON:
x =  '{ "name":"John", "age":30, "city":"New 
  York"}'

# parse x:
y = json.loads(x)

# the result is a 
  Python dictionary:
print(y["age"]) 
3

#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
>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
['foo', {'bar': ['baz', None, 1.0, 2]}]
>>> json.loads('"\\"foo\\bar"')
'"foo\x08ar'
>>> from io import StringIO
>>> io = StringIO('["streaming API"]')
>>> json.load(io)
['streaming API']
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

    json.dumps(x, indent=4, separators=(". ", " = "))
 
0

    json.dumps(x, indent=4)
 
-1

New to Communities?

Join the community