I have the a JSON file which contains {"67790": {"1": {"kwh": 319.4}}}. Now I create a dictionary a_dict which I need to append to the JSON file.
I tried this code:
with open(DATA_FILENAME, 'a') as f:
json_obj = json.dump(a_dict, json.load(f)
f.write(json_obj)
f.close()
What is wrong with the code? How can I fix the problem?
Assuming you have a test.json file with the following content:
{"67790": {"1": {"kwh": 319.4}}}
Then, the code below will load the json file, update the data inside using dict.update() and dump into the test.json file:
import json
a_dict = {'new_key': 'new_value'}
with open('test.json') as f:
data = json.load(f)
data.update(a_dict)
with open('test.json', 'w') as f:
json.dump(data, f)
Then, in test.json, you'll have:
{"new_key": "new_value", "67790": {"1": {"kwh": 319.4}}}
Hope this is what you wanted.
You need to update the output of json.load with a_dict and then dump the result.
And you cannot append to the file but you need to overwrite it.
json_obj=json.dumps(a_dict, ensure_ascii=False)
Related
I need to append template in place 'here' and I have no idea how to do that.
There's how json look like:
{
"1st_thing":
[{"1st":"1stvalue"},{"2nd":"2ndvalue"}, /*here*/],
"2nd_thing":
[{"xx":"XXvalue"},{"yy":"YYvalue"},/*here*/]
}
I've tried this and it only do this temporary but I need to save it in 'tokens.json'
with open("tokens.json") as f:
new = json.load(f)
new['captcha_tokens'].append(template)
You can use dump to write to a new file:
json.dump(new, open("tokens_new.json", "w"))
Or if you want to prettify a bit:
json.dump(new, open("tokens_new.json", "w"), ensure_ascii=True, indent=4)
Then you can replace tokens.json with shutil:
import shutil
shutil.copy("tokens_new.json", "tokens.json")
I have a Python script that updates some value of a JSON file, and the original format of my JSON looks like:
To edit the value I use this code:
import json
status_wifi = "ok"
with open("config_wifi.json", "r") as jsonFile:
data = json.load(jsonFile)
data['wifi_session']['status'] = status_wifi
with open("config_wifi.json", "w") as jsonFile:
json.dump(data, jsonFile)
But when the values are updated, the format of my JSON is compressed like this:
I want the JSON file to keep its original format with all spaces and line breaks. How could I do that?
Try json.dumps(json_obj, indent=4)
Example:
import json
status_wifi = "ok"
with open("config_wifi.json", "r") as jsonFile:
data = json.load(jsonFile)
data['wifi_session']['status'] = status_wifi
with open("config_wifi.json", "w") as jsonFile:
json.dump(json.dumps(data, indent=4), jsonFile)
The indent is the number of spaces for a tab.
If you set this parameter, the JSON will be formatted.
You can read more about it here.
I have a json file that I am using as a Dictionary in python.
The json file is really long with 10k+ records. I need to replace the $home part in the "iscategorical" with the value of "id". After making the changes, I want to save this file so that I can use it again as a dictionary. Thank you for the help. Here is a sample:
{
"maps": [
{
"id": "xyzp",
"iscategorical": "/u/$home/app/home"
},
{
"id": "trtn",
"iscategorical": "/u/app/$home/user"
}
]}
I am understanding that you are able to load the file successfully, and all you want to do is replace the strings and save the structure to file again.
For this, we can traverse the list of dictionaries in the data, and modify the value of item['iscategorical'] by replacing $home with the value of item['id'].
We can then dump the modified structure back to (a new) json file.
import json
with open('data.json') as f:
data = json.load(f)
for item in data['maps']:
item['iscategorical'] = item['iscategorical'].replace('$home', item['id'])
with open('new_data.json', 'w') as f:
json.dump(data, f)
Your question seems similar to - Parsing values from a JSON file? .
However for your case below snippet should work.
import json
with open('idata.json') as infile:
data = json.load(infile)
for elem in data["maps"]:
elem['iscategorical']=elem['iscategorical'].replace('$home',elem['id'])
with open('odata.json', 'w') as outfile:
json.dump(data, outfile)
If it's a file, one thing you can do is load the file in and read line by line.
for everyline, you can use regex to find and replace. Then you can either overwrite the file or write onto a new file.
For example,
line.replace('$home', 'id')
Alternatively, you can load the json python in and convert it into a string. Then replace the text using the regex. Finally, converts back to Python dictionary using json.load().
However, 10k line is too long. I think reading a file, line-by-line, would be a better solutions.
EDIT:
Here is the code sample.
from tempfile import mkstemp
from shutil import move
from os import fdopen, remove
def replace(file_path, pattern, subst):
#Create temp file
fh, abs_path = mkstemp()
with fdopen(fh,'w') as new_file:
with open(file_path) as old_file:
for line in old_file:
new_file.write(line.replace(pattern, subst))
#Remove original file
remove(file_path)
#Move new file
move(abs_path, file_path)
replace('./text.txt', '$home', 'id')
"The JSON file is really long with 10k+ records" -Try this way it should help for large files.
input.json
{"maps":[{"id":"xyzp","iscategorical":"/u/$home/app/home"},{"id":"trtn","iscategorical":"/u/app/$home/user"}]}
import json
with open('input.json') as f:
data = json.load(f)
my_list = []
def get_some_data():
for item in data['maps']:
yield(item['id'], item['iscategorical'])
for id, iscat in get_some_data():
temp_dict = {}
temp_dict['id'] = id
temp_dict['iscategorical'] = iscat.replace('$home', id)
my_list.append(temp_dict)
maps_dict = {}
maps_dict['maps'] = my_list
with open('output.json', 'w') as f:
json.dump(maps_dict, f)
output.json:
{"maps": [{"id": "xyzp", "iscategorical": "/u/**xyzp**/app/home"}, {"id": "trtn", "iscategorical": "/u/app/**trtn**/user"}]}
I have a dictionary, for example:
a = {'a':1,'b':2,'c':3}
And I want it to be saved in a json file.
How can I do this with the original python json library?
Please note that I am running Python 3.5.2, which has a build-in json library.
You can also dump json file directly using json.dump instead of json.dumps.
import json
a = {'a':1,'b':2,'c':3}
with open("your_json_file", "w") as fp:
json.dump(a , fp)
json.dumps is mainly used to display dictionaries as a json format with the type of string. While dump is used for saving to file. Using this to save to file is obsolete.
The previous example only save the file to json but does not make it very pretty. So instead you could do this:
json.dump(a, fp, indent = 4) # you can also do sort_keys=True as well
# this work the same for json.dumps
This makes the json file more user friendly to read. the pydoc has some good description on how to use the json module.
To retrieve your data back, you can use the load function.
a = json.load(fp) # load back the original dictionary
This may help you...
import json
a = {'name': 'John Doe', 'age': 24}
js = json.dumps(a)
# Open new json file if not exist it will create
fp = open('test.json', 'a')
# write to json file
fp.write(js)
# close the connection
fp.close()
show you code
#!/usr/bin/env python
# coding:utf-8
'''黄哥Python'''
import json
a = {'a': 1, 'b': 2, 'c': 3}
with open("json.txt", "w") as f:
f.write(json.dumps(a))
check this-
import json
a = {'a':1,'b':2,'c':3}
json_str = json.dumps(a)
with open('data.json', 'w') as f:json.dump(data, f)
Most of other answers are correct but this will also print data in prettify and sorted manner.
import json
a = {'name': 'John Doe', 'age': 24}
js = json.dumps(a, sort_keys=True, indent=4, separators=(',', ': '))
with open('test.json', 'w+') as f:
f.write(js)
I am using Python and I have a JSON file in which I would like to update a value related to a given key. That is, I have the my_file.json containing the following data
{"a": "1", "b": "2", "c": "3"}
and I would like to just change the value related to the b key from 2 to 9 so that the updated file look as like:
{"a": "1", "b": "9", "c": "3"}
How can I make that?
I tried the following but without success (the changes are not saved to the file):
with open('my_file.json', 'r+') as f:
json_data = json.load(f)
json_data['b'] = "9"
f.close()
You did not save the changed data at all. You have to first load, then modify, and only then save. It is not possible to modify JSON files in-place.
with open('my_file.json', 'r') as f:
json_data = json.load(f)
json_data['b'] = "9"
with open('my_file.json', 'w') as f:
f.write(json.dumps(json_data))
You may also do this:
with open('my_file.json', 'r+') as f:
json_data = json.load(f)
json_data['b'] = "9"
f.seek(0)
f.write(json.dumps(json_data))
f.truncate()
If you want to make it safe, you first write the new data into a temporary file in the same folder, and then rename the temporary file onto the original file. That way you will not lose any data even if something happens in between.
If you come to think of that, JSON data is very difficult to change in-place, as the data length is not fixed, and the changes may be quite significant.
You are almost there, you only have to write the updated json_data back to the file. Get rid of f.close(), as the with statement will ensure that the file is closed. Then, issue
with open('my_file.json', 'w') as f:
f.write(json.dumps(json_data))
This is simplest way to do the json file updation/writing.
where you are creating instance of json file as 'f' and the writing the 'data' into the json file,
#write json file
with open('data.json', 'w') as f:
json.dump(data, f)
#Read json file
with open('data.json', 'r') as f:
json.load(data, f)
Open the file and store in one variable all the content using json.load function
Update your key stored in the previous variable
Open the file another time and update your content with the variable updated
def updateJsonFile():
jsonFile = open("my_file.json", "r") # Open the JSON file for reading
data = json.load(jsonFile) # Read the JSON into the buffer
jsonFile.close() # Close the JSON file
## Working with buffered content
data["b"] = "9"
## Save our changes to JSON file
jsonFile = open("my_file.json", "w+")
jsonFile.write(json.dump(data))
jsonFile.close()