---
title: "Rename JSON Key based on other key's value"
date: 2025-12-30T05:27:51Z
modified: 2026-06-12T08:50:49Z
permalink: "https://ezintegrations.ai/docs/rename-json-key-using-another-key/"
type: docs
status: publish
excerpt: ""
wpid: 3061
doc_category:
  - Pycode Examples
author: Arun Thakur
---

## 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.

1. Configure the integration to receive attribute-based JSON records.
2. Open the Python Operation editor.
3. Paste the dynamic key renaming script.
4. Ensure the incoming data is available in the **\_data** variable.
5. Save and deploy the workflow.
6. 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

 ### 1. What is the purpose of using the \_data variable?

 The \_data variable holds the incoming record and allows in-place modification during processing.

 

 

  ### 2. Can multiple keys be renamed in one script?

 Yes. The logic can be extended to process multiple attributename and attributevalue pairs.

 

 

  ### 3. What happens if attributename is empty?

 An empty or null attributename may result in invalid or missing output keys.

 

 

  ### 4. Does this script remove original keys?

 Yes. The original attributename and attributevalue keys are removed and replaced.

 

 

  ### 5. 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.

## Topics

**Docs Categories:** [Pycode Examples](https://ezintegrations.ai/wp-content/uploads/wp-mfa-exports/taxonomy/doc_category/pycode-examples.md)