@nullplatform/json-keys-transform
v0.0.5
Published
A utility for transforming JSON object keys. It supports various transformations such as camelCase, snake_case, and more
Readme
Description
This library helps services to transform camel cased objects to snake cased objects and backwards
Install
npm install @nullplatform/json-keys-transformIt is published to the public npm registry and to GitHub Packages, with the same
versions: a project that resolves the whole @nullplatform scope from GitHub
Packages (@nullplatform:registry=https://npm.pkg.github.com) gets it from
there. TypeScript declarations are included.
It is the transform @nullplatform/server applies to request and response
bodies (transform.request.exclude / transform.response.exclude take the same
path patterns), so a client can undo exactly what a service does.
Using this library
You can use this library by calling the transform function directly or using one of the function helpers like snakeCaseToCamelCase or camelCaseToSnakeCase.
Please take a look to the examples below:
// CommonJS
const {
transform,
Style,
snakeCaseToCamelCase,
camelCaseToSnakeCase,
} = require('@nullplatform/json-keys-transform');
// ESM
import {
transform,
Style,
snakeCaseToCamelCase,
camelCaseToSnakeCase,
} from '@nullplatform/json-keys-transform';
// Example objects
const snakeCaseObject = {
id: 1,
name: 'John',
last_name: 'Rambo',
address: { city: 'Montevideo', street: 'Brasil', door_number: '2424' },
};
const camelCaseObject = {
id: 1,
name: 'John',
lastName: 'Rambo',
address: { city: 'Montevideo', street: 'Brasil', doorNumber: '2424' },
};
//
// Using transform function
//
let options = { from: Style.SNAKE_CASE, to: Style.CAMEL_CASE };
let result = transform(snakeCaseObject, options);
console.log(result);
// => { id: 1, name: 'John', lastName: 'Rambo', address: { city: 'Montevideo', street: 'Brasil', doorNumber: '2424' }}
options = { from: Style.SNAKE_CASE, to: Style.CAMEL_CASE, exclude: ['last_name'] };
result = transform(snakeCaseObject, options);
console.log(result);
// => { id: 1, name: 'John', last_name: 'Rambo', address: { city: 'Montevideo', street: 'Brasil', doorNumber: '2424' }}
//
// Using snake case to camel case shortcut function
//
result = snakeCaseToCamelCase(snakeCaseObject);
console.log(result);
// => { id: 1, name: 'John', lastName: 'Rambo', address: { city: 'Montevideo', street: 'Brasil', doorNumber: '2424' }}
result = snakeCaseToCamelCase(snakeCaseObject, { exclude: ['last_name'] });
console.log(result);
// => { id: 1, name: 'John', last_name: 'Rambo', address: { city: 'Montevideo', street: 'Brasil', doorNumber: '2424' }}
//
// Using camel case to snake case shortcut function
//
result = camelCaseToSnakeCase(camelCaseObject);
console.log(result);
// => { id: 1, name: 'John', last_name: 'Rambo', address: { city: 'Montevideo', street: 'Brasil', door_number: '2424' }}
result = camelCaseToSnakeCase(camelCaseObject, { exclude: ['lastName'] });
console.log(result);
// => { id: 1, name: 'John', lastName: 'Rambo', address: { city: 'Montevideo', street: 'Brasil', door_number: '2424' }}Options
The module allows configuring various options to customize the behavior of the key transformation process. Below are the different options available:
from: Specifies the case style of the input keys. It must be one of the predefined Style options.to: Specifies the case style to which the keys should be transformed. It must be one of the predefined Style options.exclude: An array of path patterns that should be excluded from transformation. Paths can include wildcards (*). For example,['address.[*].door_number']would exclude alldoor_numberkeys under theaddressarray from transformation.
