json-key-formatter

1.0.3 • Public • Published

🌟 JSON Key Formatter

Description

This package allows you to replace the keys found in a JSON object with the provided values.

Features

  • Replaces keys in an object with the given values.
  • Supports nested objects.
  • Supports array values.

Installation

npm install json-key-formatter

Usage

import { transformKeysInObject } from 'json-key-formatter';
//OR
const { transformKeysInObject } = require('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"
  }
}

Thanks!

Thanks for choosing to use this package! If you have any questions or feedback, please feel free to let me know. 🙏

Package Sidebar

Install

npm i json-key-formatter

Weekly Downloads

141

Version

1.0.3

License

MIT

Unpacked Size

5.1 kB

Total Files

5

Last publish

Collaborators

  • batorovic