Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Append to a json file using python

Trying to append to a nested json file

My goal is to append some values to a JSON file.

Here is my original JSON file

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
    "web": {
        "all": {
          "side": {
            "tags": [
              "admin"
            ],
            "summary": "Generates",
            "operationId": "Key",
            "consumes": [],
            "produces": [
              "application/json"
            ],
            "responses": {
              "200": {
                "description": "YES",
                "schema": {
                  "type": "string"
                }
              }
            },
            "Honor": [
              {
                "presidential": []
              }
            ]
          }
        }

    }
}

It is my intention to add two additional lines inside the key "Honor", with the values "Required" : "YES" and "Prepay" : "NO". As a result of appending the two values, I will have the following JSON file.

{
    "web": {
        "all": {
          "side": {
            "tags": [
              "admin"
            ],
            "summary": "Generates",
            "operationId": "Key",
            "consumes": [],
            "produces": [
              "application/json"
            ],
            "responses": {
              "200": {
                "description": "YES",
                "schema": {
                  "type": "string"
                }
              }
            },
            "Honor": [
              {
                "presidential": [],
                "Required" : "YES",
                "Prepay"   : "NO"
              }
            ]
          }
        }

    }
}

Below is the Python code that I have written

import json
def write_json(data,filename ="exmpleData.json"):
    with open(filename,"w") as f:
        json.dump(data,f,indent=2)
with open ("exmpleData.json") as json_files:
    data= json.load(json_files)
    temp = data["Honor"]
    y = {"required": "YES","type": "String"}
    temp.append(y)
write_json(data)

I am receiving the following error message:

** temp = data["Honor"] KeyError: ‘Honor’
**
I would appreciate any guidance that you can provide to help me achieve my goal. I am running Python 3.7

>Solution :

'Honor' deeply nested in other dictionaries, and its value is a 1-element list containing a dictionary. Here’s how to access:

import json

def write_json(data, filename='exmpleData.json'):
    with open(filename, 'w') as f:
        json.dump(data, f, indent=2)

with open('exmpleData.json') as json_files:
    data = json.load(json_files)
    # 'Honor' is deeply nested in other dictionaries
    honor = data['web']['all']['side']['Honor']
    # Its value is a 1-element list containing another dictionary.
    honor[0]['Required'] = 'YES'
    honor[0]['Prepay'] = 'NO'

write_json(data)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading