Overview
Renaming JSON keys dynamically based on the value of another key is a common data transformation requirement in integration workflows.
In eZintegrations, Python Operations can be used to replace an existing key with a new key derived from another fieldโs value, enabling flexible and dynamic data restructuring.
When to Use
Use this method when key names in a dataset must be generated dynamically from existing field values.
- Transforming attribute-based datasets
- Normalizing product or metadata records
- Creating dynamic field mappings
- Improving data readability
- Customizing output formats for APIs
How It Works
The script removes the attributename and attributevalue keys from the incoming record.
The value of attributename is used as the new key name, and the value of attributevalue becomes its corresponding value.
The transformed key-value pair is then added back to the original JSON object.
Input Data
The Python Operation receives attribute records in multiline JSON format.
{
"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"
}
Python Operation Logic
When manipulating incoming data, the _data variable contains the current JSON record being processed.
The following script dynamically renames keys using values from existing fields.
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"})
Output Data
After applying the script, the original keys are replaced with dynamically generated keys.
{
"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"
}
How to Use
Follow these steps to rename JSON keys dynamically.
- Configure the integration to receive attribute-based JSON records.
- Open the Python Operation editor.
- Paste the dynamic key renaming script.
- Ensure the incoming data is available in the _data variable.
- Save and deploy the workflow.
- Test the transformation using sample input.
Use Case Example
This method is useful for transforming attribute records into flattened structures.
- Input: attributename = SKU, attributevalue = 999898
- Output: “SKU”: “999898”
- Usage: Product data normalization
Troubleshooting
- Ensure attributename and attributevalue keys exist in input.
- Verify that _data contains a valid dictionary.
- Check for null or empty attribute values.
- Confirm that pop() operations do not remove required fields.
- Review logs if renamed keys are missing.
Frequently Asked Questions
What is the purpose of using the _data variable?
The _data variable holds the incoming record and allows in-place modification during processing.
Can multiple keys be renamed in one script?
Yes. The logic can be extended to process multiple attributename and attributevalue pairs.
What happens if attributename is empty?
An empty or null attributename may result in invalid or missing output keys.
Does this script remove original keys?
Yes. The original attributename and attributevalue keys are removed and replaced.
Can I keep the original keys as well?
Yes. Remove the pop() statements if you want to retain the original fields.
Notes
- This method assumes consistent attribute naming.
- Avoid special characters in dynamically generated keys.
- Validate attribute values before transformation.
- Test scripts in a staging environment.
- Follow organizational data standards.