Search code examples
jsonpython-3.xdictionarynested

python 3 how to add key:value to nested dict in json


I am trying to create a json file for TL1 commands. The format I was given is:

{
"data": {
    "nodes": [
        {
            "deviceType": "6500",
            "nodeName": "hostNameHere",
            "Shelf Type": "Transponder",
            "shelves": [
                {
                    "primary": "DISABLE",
                    "scripts": [
                        {
                            "commands": [{}]
                        }
                    ]
                }
            ]
        }
    ]
}
}

and I am ending up with

{
"data": {
    "nodes": [
        {
            "deviceType": "6500",
            "nodeName": "hostNameHere",
            "Shelf Type": "Transponder",
            "shelves": [
                {
                    "primary": "DISABLE",
                    "scripts": [
                        {
                            "commands": [
                                {}
                            ]
                        }
                    ]
                }
            ]
        }
    ]
},
"command": "SET-SID:GenTid1::CTAG::\"hostNameHere\";",
"CommandType": "tl1"
}

I am needing the command and commandType under "commands:"[] have tried update, append even tried to put the command and command type directly into the dict

 {
                             "commands": [
{
   "command": "SET-SID:GenTid1::CTAG::\"hostNameHere\";",
"CommandType": "tl1"}    ]
                                 
                             }

but comes up with a type set error

Any suggestions on how to get the command in the proper place?

my code:

root = tk.Tk()
root.attributes("-topmost", True)
root.withdraw()
file = tkinter.filedialog.askopenfilename()

Dir_Path = (os.path.dirname(file) )
fName = os.path.basename(Dir_Path)
def TL1_script (TL1_Command):
    fReport = open ("%s\%s_TL1.txt" %(Dir_Path, fName), "a" )
    fReport.write("\"command\": \"%s\",\r" %(TL1_Command) )
    fReport.close()

def Type_script (CommandType):
    fReport = open ("%s\%s_TL1.txt" %(Dir_Path, fName), "a" )
    fReport.write("\"commandType\": \"%s\"\r" %(CommandType) )
    fReport.close()

TL1 = ("SET-SID:GenTid1::CTAG::\"%s\";" %(TID) )
CommandType = ("tl1")
TL1_Command = TL1_script(TL1)
Type = Type_script (CommandType)



i= {"data": {
        "nodes": [
            {
                "deviceType": "6500",
                "nodeName": "%s" %(TID),
                "Shelf Type": "%s" %(Shelf_Type),
                "shelves": [
                    {"primary": "%s" %(Primary),
                     "scripts": [
                         {
                             "commands": [{}]
                                 
                             }
                         ]
                     }
                ]
            }
        ]
    }
}

key = {"command"}
value = ["%s" %(TL1)]
key1 = ["CommandType"]
value1 = ["%s" %(CommandType)]

i.update(dict(zip(key, value)))
i.update(dict(zip(key1, value1)))

with open("%s\%s_TL1.json" %(Dir_Path, fName), "a") as outfile:
    json.dump(i, outfile, indent=4)

print(json.dumps(i, indent=8))

I have a text file with all of the key value pairs for the TL1 commands that I am needing to add to the json file under that commands section


Solution

  • You need to direct the update to the correct position in the dictionary

    key = "command"
    value = ["%s" %(TL1)]
    key1 = "CommandType"
    value1 = ["%s" %(CommandType)]
    
    i["data"]["nodes"][0]["shelves"][0]["scripts"][0]["commands"] = {key:value, key1:value1}
    
    with open("%s\%s_TL1.json" %(Dir_Path, fName), "a") as outfile:
        json.dump(i, outfile, indent=4)
    print(json.dumps(i, indent=8))