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.

5
  • You want to apply update on version history to any specific product or all the products? Commented Dec 21, 2016 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. Commented Dec 21, 2016 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). Commented Dec 21, 2016 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. Commented Dec 21, 2016 at 16:25
  • @Moinuddin Quadri, it can be either specific product or all products depending upon use cases. Commented Dec 27, 2016 at 7:53

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.