I'm trying to append some data into a JSON file inside python. It should add the CVE-ID into my customer JSON file.
I wasn't able to solve this by myself. If I print my dict after appending it shows the expected result.
But it seems, that the file will not be written after the append. I have also tried to use json.dump() after the "appending-line" - but also without success.
At the moment my code looks like this:
with open("test.json", "r+") as customerdata:
customers_json = json.load(customerdata)
# some other code here...
if cve["cve"]["CVE_data_meta"]["ID"] not in customer["customer"]["already-sent-cve"]:
customers_json["customers"][0]["customer"]["already-sent-cve"].append(cve["cve"]["CVE_data_meta"]["ID"])
My JSON file looks like this:
{
"customers":[
{
"customer":{
"id":"1",
"company-name":"test GmbH",
"alert-email":"[email protected]",
"using":[
"xxx",
"xyz"
],
"already-sent-cve":[
"CVE-2013-3738"
# here new CVE
]
}
},
{
"customer":{
"id":"2",
...
}
]
}
FYI: There is another JSON file with the CVE's from which file I'm collecting the data.
Any suggestions how to solve this?
Regards.
EDIT:
Was able to solve this:
with open("test.json", "w") as customerdata:
customers_json["customers"][0]["customer"]["already-sent-cve"].append(cve["cve"]["CVE_data_meta"]["ID"])
json.dump(customers_json, customerdata, indent=2)
json.dump