rename-json-keys
v1.4.1
Published
Recursively rename JSON keys using a mapping dictionary or spec without changing data structure.
Maintainers
Readme
rename-json-keys
Recursively rename JSON keys using a simple dictionary map without changing data structures or values.
🔗 Quick Links
- 📖 Documentation: https://keshavsoft.github.io/rename-json-keys/
- 🎮 Interactive Playground: https://keshavsoft.github.io/rename-json-keys/playground.html
- 📦 npm Package: https://www.npmjs.com/package/rename-json-keys
- 🐙 GitHub Repository: https://github.com/keshavsoft/rename-json-keys
Why rename-json-keys?
JavaScript has no native way to recursively rename keys in deeply nested objects and arrays without writing manual traversal code or mutating your original data.
rename-json-keys gives you:
- Truly Recursive: Renames keys everywhere in arrays and nested objects.
- Non-Destructive: Never mutates input data; data structure and order remain intact.
- Zero Dependencies: Lightweight and fast.
- Simple Dictionary Map: Just specify
{ "oldKey": "newKey" }.
Installation
npm install rename-json-keysQuick Start (5 Seconds)
import { renameJsonKeys } from "rename-json-keys";
const user = {
user_id: 101,
first_name: "Alice",
contact_info: {
email_addr: "[email protected]",
phone_no: "+1-555-0199"
}
};
const keyMap = {
user_id: "id",
first_name: "firstName",
contact_info: {
email_addr: "email",
phone_no: "phone"
}
};
const result = renameJsonKeys(user, keyMap);
console.log(result);Output:
{
"id": 101,
"firstName": "Alice",
"contact_info": {
"email": "[email protected]",
"phone": "+1-555-0199"
}
}Options Object Convention
Also supports the KeshavSoft single-object argument convention:
const result = renameJsonKeys({
inData: user,
inKeys: keyMap
});Advanced: Deeply Nested Enterprise Data
Handles multi-level enterprise structures (like accounting vouchers, orders, or logistics manifests) seamlessly:
const vouchers = [
{
DATE: "20260401",
VOUCHERNUMBER: 1,
"ALLINVENTORYENTRIES.LIST": [
{
STOCKITEMNAME: "Shading Net Kgs",
AMOUNT: 1280.9,
"BATCHALLOCATIONS.LIST": [
{
MFDON: 20210723,
GODOWNNAME: "Main Location",
AMOUNT: 1000
}
]
}
]
}
];
const keyMap = {
DATE: "date",
VOUCHERNUMBER: "voucherNumber",
"ALLINVENTORYENTRIES.LIST": "inventoryEntries",
STOCKITEMNAME: "stockItemName",
AMOUNT: "amount",
"BATCHALLOCATIONS.LIST": "batchAllocations",
MFDON: "mfdOn",
GODOWNNAME: "godownName"
};
const clean = renameJsonKeys(vouchers, keyMap);Pipeline Role
Used as Step 2 in the KeshavSoft declarative data suite:
Tally XML ➔ select-json-by-json ➔ rename-json-keys ➔ normalize-json-by-json ➔ map-json-by-json