0

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.

Community
  • 1
  • 1
RIPAN
  • 3,326
  • 4
  • 17
  • 28
  • You want to apply update on version history to any specific product or all the products? – Moinuddin Quadri Dec 21 '16 at 16:20
  • 1
    SO is not a code writing service. Please show us what you've tried by posting your code and and tell us where you got stuck. – Mike Scotty Dec 21 '16 at 16:20
  • What have you tried that didn't work ??? (hint: it's so trivial that you would probably have spend less time solving it by yourself than posting this). – bruno desthuilliers Dec 21 '16 at 16:21
  • Also, your dictionary keys are not an arrays... your array members are dictionaries though... so just append another one... should be easy. – user3012759 Dec 21 '16 at 16:25
  • @Moinuddin Quadri, it can be either specific product or all products depending upon use cases. – RIPAN Dec 27 '16 at 07:53

1 Answers1

1

You could try something like:

for obj in metadata["products"]:
    if obj["productName"]==nameToLookFor:
        obj["versionHistory"].append(newObject)

this will add the version to the product with the name you specify in nameToLookFor

Buzz
  • 1,877
  • 21
  • 25