Skip to main content

Rename JSON Key based on other key's value

In the below python code examples user can dynamically change JSON key based on other key's value

Input Data with multiline json
{
                "attributeid": 212077,
                "attributename": "SKU",
                "attributevalue": "999898",
                "attribute_groupid": 24315,
                "attribute_groupname": "Default",
                "Isvariant": "false"
            }
            {
                "attributeid": 212078,
                "attributename": "Product Name",
                "attributevalue": "Pim Item Update Testing",
                "attribute_groupid": 24315,
                "attribute_groupname": "Default",
                "Isvariant": "false"
            }
Below is the pycode ops
keyname = ["attributename"]
keyvalue = ["attributevalue"]
for i in keyname:
    attributename = _data.pop(i, None)
for j in keyvalue:
    attributevalue = _data.pop(j, None)
_data[attributename] = attributevalue
_data.update({"new_key":"new_value"})

Note: In the above example as we want to do some manipulation in the incoming data so for that we have used _data variable as this _data holds your incoming data and based on that we can do the manipulation in your data using pycode script.

Output data after applying Python Pycode
{
                "attributeid": 212077,
                "SKU": "999898",
                "attribute_groupid": 24315,
                "attribute_groupname": "Default",
                "Isvariant": "false"
            }
            {
                "attributeid": 212078,
                "Product Name": "Pim Item Update Testing",
                "attribute_groupid": 24315,
                "attribute_groupname": "Default",
                "Isvariant": "false"
            }