skip to Main Content

I’ve got a JSON file similar to this:

{
    "Name": "a1",
    "Notes": "b1",
    "Active": "c1"
},
{
    "Name": "a2",
    "Notes": "b2",
    "Active": "c2"
},
{
    "Name": "a3",
    "Notes": "b4",
    "Active": "c3"
}

I need to transform it using Python by adding a progressive "index"/"id" for every data block.
This is the desired output (notice that there are no quotes around the "id" value):

{
    "id": 0,
    "Name": "a1",
    "Notes": "b1",
    "Active": "c1"
},
{
    "id": 1,
    "Name": "a2",
    "Notes": "b2",
    "Active": "c2"
},
{
    "id": 2,
    "Name": "a3",
    "Notes": "b4",
    "Active": "c3"
}

If it might be useful I’m building this JSON by converting a CSV using the code found here: https://stackoverflow.com/a/66071962/4712710

3

Answers


  1. Here is an example how you can add new keys to each dictionary using enumerate():

    import json
    
    original_json_text = """[
        {"Name": "a1", "Notes": "b1", "Active": "c1"},
        {"Name": "a2", "Notes": "b2", "Active": "c2"},
        {"Name": "a3", "Notes": "b4", "Active": "c3"}
    ]"""
    
    data = json.loads(original_json_text)
    
    data = [{"id": i, **d} for i, d in enumerate(data)]
    
    print(json.dumps(data, indent=4, sort_keys=False))
    

    Prints:

    [
        {
            "id": 0,
            "Name": "a1",
            "Notes": "b1",
            "Active": "c1"
        },
        {
            "id": 1,
            "Name": "a2",
            "Notes": "b2",
            "Active": "c2"
        },
        {
            "id": 2,
            "Name": "a3",
            "Notes": "b4",
            "Active": "c3"
        }
    ]
    
    Login or Signup to reply.
  2. You can do this fairly simply with the JSON library.

    import json
    
    json_array = json.loads('''
        [
            {
                "Name": "a1",
                "Notes": "b1",
                "Active": "c1"
            },
            {
                "Name": "a2",
                "Notes": "b2",
                "Active": "c2"
            },
            {
                "Name": "a3",
                "Notes": "b4",
                "Active": "c3"
            }
        ]
    ''')
    
    for i, d in enumerate(json_array):
        d["id"] = i
    
    print(json.dumps(json_array))
    

    This parses the json code into a python object, and then iterates through the array and adds the id field.

    Login or Signup to reply.
  3. Full example, with some comments:

    import json
    
    # with open is important to close the file regardless of result 
    with open('my_file.json') as f:
        # here we read the file and use json module to unpack
        content = json.loads(f.read())
    
    # list comprehension, merge 2 dictionaries and enumerate, you can read more about those)
    content = [{"id": index, **obj} for index, obj in enumerate(content)]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search