Home > Mobile >  Replace value of a specific key in json file for multiple files
Replace value of a specific key in json file for multiple files

Time:11-28

I am preparing a test data for the feature testing . I have 1000 json file with same below structure in local folder .

I need to replace two key from all the places and create 1000 different json . Key that needs to be replaced is "f_ID": "80510926" and "f_Ctb": "10333" "f_ID": "80510926" appears in all the array but "f_Ctb": "10333" appear only once .

The replaced value can be running number 1 to 100 in all files .

Can some one suggest how we can do this python to create 1000 files

{
  "t_A": [
    {
      "f_ID": "80510926",
      "f_Ctb": "10333",
      "f_isPasswordProtected": "False"
    }
  ],
  "t_B": [
    {
      "f_ID": "80510926",
      "f_B_Company_ID": "10333",
      "f_B_ID": "1",
      "f_ArriveDate": "20151001 151535.775"
    },
    {
      "f_ID": "80510926",
      "f_B_Company_ID": "10333",
      "f_B_ID": "1700",
      "f_ArriveDate": "20151001 151535.775"
    }
  ],
  "t_C": [
    {
      "f_ID": "80510926",
      "f_Set_Code": "TRBC      ",
      "f_Industry_Code": "10        ",
      "f_InsertDate": "20151001 151535.775"
    },
  ],
  "t_D": [
    {
      "f_ID": "80510926",
      "f_Headline": "Global Reinsurance: Questions and themes into Monte Carlo",
      "f_Synopsis": ""
    }
  ]
}

CodePudding user response:

here is the solution run in the folder where you have 1000 json files. It will read all the json files and replace the f_ID and f_Ctb with the count and write the file with same file name in output folder.

import os
import json

all_files = os.listdir()
json_files = {f: f for f in all_files if f.endswith(".json")}
json_files_keys = list(json_files.keys())
json_files_keys.sort()

OUTPUT_FOLDER = "output"
if not os.path.exists(OUTPUT_FOLDER):
    os.mkdir(OUTPUT_FOLDER)

for file_name in json_files_keys:
    f_read = open(file_name, "r").read()
    data = json.loads(f_read)
    output_data = {}
    count = 1
    for key, val_list in data.items():
        for nest_dict in val_list:
            if "f_ID" in nest_dict:
                nest_dict["f_ID"] = count
            if "f_Ctb" in nest_dict:
                nest_dict["f_Ctb"] = count
            if key in output_data:
                output_data[key].append(nest_dict)
            else:
                output_data[key] = [nest_dict]
        else:
            output_data[key] = val_list
        count  = 1
    output_file = open(f"{OUTPUT_FOLDER}/{file_name}", "w")
    output_file.write(json.dumps(output_data))
    output_file.close()
  • Related