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?

halfer
  • 19,824
  • 17
  • 99
  • 186
smatthewenglish
  • 2,831
  • 4
  • 36
  • 72
  • Possible duplicate of [Python read JSON file and modify](https://stackoverflow.com/questions/21035762/python-read-json-file-and-modify) – Hamms Nov 16 '17 at 23:44
  • Not sure if appropriate to mention, but I wrote a library to do this: https://github.com/erewok/pelecanus You may be able to get some inspiration from it? – erewok Nov 16 '17 at 23:50
  • So, is the JSON arbitrarily nested? Or is this exactly how it is? – cs95 Nov 17 '17 at 00:00
  • Also, can the JSON contain lists, or only dicts? – cs95 Nov 17 '17 at 00:01

1 Answers1

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)
63677
  • 282
  • 3
  • 7