1

I would like to access all the keys including within lists in a JSON document and modify them.

I have the following code, but this just prints the top level keys:

#!/usr/bin/env python3

import json

file = 'example.json'
f = open(file)
data = json.load(f)

for key, value in data.items():
        print(key)

Thus, for the JSON document:

{
"a":1,
"b":2,
"c":[
     {
     "d":4,
     "e":5,
     "f":{
         "g":6
         }
     }
    ]
}

I just get the output:

a
b
c

whereas I want to access and amend all the keys, namely:

a
b
c
d
e
f
g

Note that c is a list, which hides d e f g.

1
  • You need a recursive approach here. Commented Nov 4, 2021 at 13:26

1 Answer 1

3

You will need some sort of recursion to traverse the data:

def nested_keys(obj):
    if isinstance(obj, dict):
        for k, v in obj.items():
            yield k
            yield from nested_keys(v)
    elif isinstance(obj, list):
        for sub in obj:
            yield from nested_keys(sub)

>>> [*nested_keys(data)]
['a', 'b', 'c', 'd', 'e', 'f', 'g']

To rename keys:

def rename_keys(obj, mapping):
    if isinstance(obj, dict):
        return {mapping[k]: rename_keys(v, mapping) for k, v in obj.items()}
    if isinstance(obj, list):
        return [rename_keys(sub, mapping) for sub in obj]
    return obj

rename_keys(data, dict("a1 b2 c3 d4 e5 f6 g6".split()))
# {'1': 1, '2': 2, '3': [{'4': 4, '5': 5, '6': {'6': 6}}]}
Sign up to request clarification or add additional context in comments.

8 Comments

Plus one - problem is that you possibly cannot do anything with the nested keys (one is not able to get the value with the key names alone, that is).
Thanks @user2390182, that prints out all the keys. How would I change the key names? I have tried defining a new global dictionary data2 = { } and adding data2[k + '0'] = obj[k] after the for k, v in obj.items: line, but the result is data2 = {'a0': 1, 'b0': 2, 'c0': [{'d': 4, 'e': 5, 'f': {'g': 6}}], 'd0': 4, 'e0': 5, 'f0': {'g': 6}, 'g0': 6}. That is, the new keys have been added to the list, but the old keys have not been deleted.
Can you provide some hints with regard to the logic of the renaming
I have a Python dictionary with old key names and new key names. Ideally I want to replace the key names in situ, but I could also create a new JSON document from the existing JSON document.
Added an example to create a new dict with keys replaced by their substitute taken from a mapping.
|

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.