1

At the moment I'm working with a large set of JSON files of the following form:

File00, at time T1:

{
  "AAA": {
    "BBB": {
      "000": "value0"
    },
    "CCC": {
      "111": "value1",
      "222": "value2",
      "333": "value3"
    },
    "DDD": {
      "444": "value4"
    }
}

It's the situation that now I have a new input for the sub-field "DDD", I'd like to "wholesale" replace it with the following:

    "DDD": {
      "666": "value6",
      "007": "value13"
    }

Accordingly the file would be changed to:

File00, at time T2:

{
  "AAA": {
    "BBB": {
      "000": "value0"
    },
    "CCC": {
      "111": "value1",
      "222": "value2",
      "333": "value3"
    },
    "DDD": {
      "666": "value6",
      "007": "value13"
    }
}

In the situation I'm confronted with, there are many files similar to File00, so I'm endeavoring to create a script that can process all the files in a particular directory, identifying the JSON field DDD and replace it's contents with something new.

How to do this in Python?

4
  • Possible duplicate of Python read JSON file and modify Commented Nov 16, 2017 at 23:44
  • Not sure if appropriate to mention, but I wrote a library to do this: github.com/erewok/pelecanus You may be able to get some inspiration from it? Commented Nov 16, 2017 at 23:50
  • So, is the JSON arbitrarily nested? Or is this exactly how it is? Commented Nov 17, 2017 at 0:00
  • Also, can the JSON contain lists, or only dicts? Commented Nov 17, 2017 at 0:01

1 Answer 1

2

Here are the steps I took for each file:

  1. Read the json
  2. Convert it to a Python dict
  3. Edit the Python dict
  4. Convert it back to json
  5. Write it to the file
  6. Repeat

Here is my code:

import json

#list of files.
fileList = ["file1.json", "file2.json", "file3.json"]

for jsonFile in fileList:
    # Open and read the file, then convert json to a Python dictionary
    with open(jsonFile, "r") as f:
        jsonData = json.loads(f.read())

    # Edit the Python dictionary
    jsonData["AAA"]["DDD"]={"666": "value6","007": "value13"}

    # Convert Python dictionary to json and write to the file
    with open(jsonFile, "w") as f:
        json.dump(jsonData, f)

Also, I got code for iterating through a directory from here. You probably want something like this:

import os

directory = os.fsencode(directory_in_str)

for file in os.listdir(directory):
    filename = os.fsdecode(file)
    if filename.endswith(".json"): 
        fileList.append(filename)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.