Skip to content Skip to sidebar Skip to footer

How To Find And Replace A Part Of A Value In Json File

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 valu

Solution 1:

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 indata['maps']:
    item['iscategorical'] = item['iscategorical'].replace('$home', item['id'])

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

Solution 2:

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 indata["maps"]:
  elem['iscategorical']=elem['iscategorical'].replace('$home',elem['id'])

with open('odata.json', 'w') as outfile:
    json.dump(data, outfile)

Solution 3:

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

defreplace(file_path, pattern, subst):
    #Create temp file
    fh, abs_path = mkstemp()
    with fdopen(fh,'w') as new_file:
        withopen(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')

Solution 4:

"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
withopen('input.json') as f:
    data = json.load(f)
my_list = []

defget_some_data():
    for item in data['maps']:
        yield(item['id'], item['iscategorical'])

forid, 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
withopen('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"}]}

Post a Comment for "How To Find And Replace A Part Of A Value In Json File"