Registry / devops / json-key-formatter

json-key-formatter

JSON →
library1.0.3jsnpmunverified

A lightweight utility that replaces keys in a JSON object based on a provided mapping, supporting nested keys and arrays. Stable version 1.0.3, minimal dependencies. Differentiator: simple API focused solely on key renaming, unlike more comprehensive object transformation libraries.

npm install json-key-formatter
INSTALL
IMPORT
SIG · JSON-KEY-FORMATTER
J
json-key-formatter
devopsjavascriptv1.0.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

transformKeysInObject
import { transformKeysInObject } from 'json-key-formatter'
import transformKeysInObject from 'json-key-formatter'
Named export only; no default export exists.
transformKeysInObject
const { transformKeysInObject } = require('json-key-formatter')
const transformKeysInObject = require('json-key-formatter')
CommonJS destructuring required; the module does not assign to module.exports directly.
TypeScript type
import type { TransformKeysInObject } from 'json-key-formatter'
import { TransformKeysInObject } from 'json-key-formatter'
TypeScript types may be available in future versions; check the package's types field.

Shows how to rename keys in a nested object using a mapping with dotted path support.

import { transformKeysInObject } from 'json-key-formatter'; const obj = { _id: 1, name: 'John Doe', age: 30, address: { street: '123 Main Street', city: 'Anytown', state: 'CA', zip: '12345', }, }; const keyMappings = { _id: 'id', name: 'fullName', 'address.city': 'location', }; const transformedObj = transformKeysInObject(obj, keyMappings); console.log(transformedObj); // Output: // { // id: 1, // fullName: 'John Doe', // age: 30, // address: { // street: '123 Main Street', // location: 'Anytown', // state: 'CA', // zip: '12345' // } // }
Debug
Known issues
gotchaThe function mutates the original object? No, it creates a new object, but arrays inside are not deeply cloned.
fix
Ensure array elements are not objects referencing the same memory if mutating.
affects: >=1.0.0
gotchaKey mappings with dots are treated as nested paths; to rename a key containing a dot, you must escape it or use a workaround.
fix
There is no built-in escape; consider prefixing or replacing the dot with a placeholder before mapping.
affects: >=1.0.0
gotchaValue type of the mapping object must be a string; non-string values are ignored or may cause errors.
fix
Ensure all mapping values are strings representing the new key names.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: transformKeysInObject is not a function
Importing as default instead of named export.
fix
Use `import { transformKeysInObject } from 'json-key-formatter'`
Cannot read properties of undefined (reading 'split')
Passing a non-object as the first argument.
fix
Ensure the first argument is a plain object or array.
Uncaught Error: Invalid key mapping
Mapping value is null or undefined.
fix
Provide a valid string for each mapping value.
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Resources
json-key-formatter — npm install json-key-formatter · libregistry