'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 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"
    }
]}


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

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

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

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

Solution 3:[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

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

Solution 4:[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
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"}]}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sal
Solution 2
Solution 3
Solution 4 Rakshith