2

I have a lot of json files, their structure is different. I need to change the value of a field in json every time, the values of other fields remain unchanged.

Now I have been able to dynamically get the path code of json as follows

def get_paths(source):
    paths = []
    if isinstance(source, collections.MutableMapping):  # found a dict-like structure...
        for k, v in source.items():  # iterate over it; Python 2.x: source.iteritems()
            paths.append([k])  # add the current child path
            paths += [[k] + x for x in get_paths(v)]  # get sub-paths, extend with the current
    # else, check if a list-like structure, remove if you don't want list paths included
    elif isinstance(source, collections.Sequence) and not isinstance(source, str):
        for i, v in enumerate(source):
            paths.append([i])
            paths += [[i] + x for x in get_paths(v)]  # get sub-paths, extend with the current
    return paths

One of the json examples is as follows, it is just one of many json:

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages...",
                        "GlossSeeAlso": [
                            "GML",
                            "XML"
                        ]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

Use the following code to get the paths

def loadJson():
    try:
        with open('../json/test1.json', 'r') as loadf:
            load_dict = json.load(loadf)
            return load_dict
    except Exception as e:
        raise Exception("load json fail")

t_json = loadJson()
paths = get_paths(loadJson())

The path in this example is as follows:

[['glossary'],
 ['glossary', 'title'],
 ['glossary', 'GlossDiv'],
 ['glossary', 'GlossDiv', 'title'],
 ['glossary', 'GlossDiv', 'GlossList'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'ID'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'SortAs'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossTerm'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'Acronym'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'Abbrev'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef', 'para'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef', 'GlossSeeAlso'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef', 'GlossSeeAlso', 0],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef', 'GlossSeeAlso', 1],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossSee']]

The question now is how to dynamically modify the value of one of the fields by the path obtained?

Example: I want to modify "title": "S" to "title": "M".

How to use ['glossary', 'GlossDiv', 'title'] to do it?

for path in paths:
   # How to get t_json[path[0]][path[1]][path[2]]?
martineau
  • 119,623
  • 25
  • 170
  • 301
andylei
  • 53
  • 2

2 Answers2

1

How to get t_json[path[0]][path[1]][path[2]]?

Simple: we just need to iterate over path, applying one indexing operation at a time. This requires that we remember our progress after each step, and the simplest way is to just reuse a variable that traces its way through the path. Thus, for example:

element = t_json
for path_item in path:
    element = element[path_item]
Malekai
  • 4,765
  • 5
  • 25
  • 60
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
1

Here's the basic idea of how to do it with the path that is a sequence of keys:

from functools import reduce

# From https://stackoverflow.com/a/28225747/355230
def recursive_get(d, *keys):
    return reduce(lambda c, k: c.get(k, {}), keys, d)

t_json = loadJson()
path = ['glossary', 'GlossDiv', 'title']
sub_dict = recursive_get(t_json, *path[:-1])
sub_dict['title'] = 'M'
print(t_json)
martineau
  • 119,623
  • 25
  • 170
  • 301