I have already seen this post Add key to a dictionary in Python but i am running now on a typical scenario where key is a array and i want to add new value to that array. Here is my condition now.
I am reading a database column which contains a json string and i have to modify the string(add some additional values to the string and keep in json format) and update the column with updated json string. Json structure is like the following
{
"tenantCreationDate": 1476869059806,
"rdVersion": "2016a06.01",
"products": [{
"productName": "Core",
"currentVerison": "2016a06.01",
"versionHistory": [{
"startDate": 1476869059829,
"upgradedBy": "squire",
"version": "2016a06.01",
"endDate": null
}]
}, {
"productName": "Capture",
"currentVerison": "2016a06.01",
"versionHistory": [{
"startDate": 1476869059829,
"upgradedBy": "squire",
"version": "2016a06.01",
"endDate": null
}]
}, {
"productName": "Vault",
"currentVerison": "2016a06.01",
"versionHistory": [{
"startDate": 1476869059829,
"upgradedBy": "squire",
"version": "2016a06.01",
"endDate": null
}]
}, {
"productName": "LVA",
"currentVerison": "2016a06.01",
"versionHistory": [{
"startDate": 1476869059829,
"upgradedBy": "squire",
"version": "2016a06.01",
"endDate": null
}]
}, {
"productName": "Correspondence",
"currentVerison": "2016a06.01",
"versionHistory": [{
"startDate": 1476869059829,
"upgradedBy": "squire",
"version": "2016a06.01",
"endDate": null
}]
}]
}
As you can see products is an array under which versionHistory is another array and i want to add another version to this array and post it to database.So what i did first is to read the column from db and converted to json
metadata = json.loads(json.dumps(row));
Now my question is how i will add another version to the array and submit the updated data to the database column and let say after update version history should look like --
"versionHistory": [{
"startDate": 1476869059829,
"upgradedBy": "squire",
"version": "2016a06.01",
"endDate": null
},
{
"startDate": 12345678,
"upgradedBy": "admin",
"version": "2016.07.02",
"endDate": 123456
}
]
Your help will be much appreciated.