0.1.0-beta.1 • Published 13 days ago

@kishor82/jsonflat v0.1.0-beta.1

Weekly downloads
-
License
MIT
Repository
github
Last release
13 days ago

NPM Version Build status Coverage Status


jsonflat

jsonflat is a lightweight Node.js library that flattens nested JSON objects to a single level using customizable delimiters.

Installation

You can install jsonflat via npm:

npm install @kishor82/jsonflat

Usage

To flata nested JSON object, import the flat function from jsonflat and use it as follows:

const { flat } = require("@kishor82/jsonflat");

const nestedObject = {
  person: {
    name: {
      first: "John",
      last: "Doe",
    },
    age: 30,
    address: {
      city: "New York",
      state: "NY",
    },
  },
};

const flattenedObject = flat(nestedObject);

console.log(flattenedObject);

The flat function accepts an optional delimiter parameter to customize the separator used in the flattened keys:

const flattenedObject = flat(nestedObject, ".");

console.log(flattenedObject);

API

flat(inputObject, delimiter = '.')

Flattens the input JSON object to a single level.

  • inputObject (Object): The nested JSON object to be flattened.
  • delimiter (String): Optional. The delimiter used to separate keys in the flattened output. Default is '.'.

Returns: A new object with flattened keys.

Examples

flatNested JSON Object

const { flat } = require("@kishor82/jsonflat");

const nestedObject = {
  person: {
    name: {
      first: "John",
      last: "Doe",
    },
    age: 30,
    address: {
      city: "New York",
      state: "NY",
    },
  },
};

const flattenedObject = flat(nestedObject);

console.log(flattenedObject);

Output:

{
  "person.name.first": "John",
  "person.name.last": "Doe",
  "person.age": 30,
  "person.address.city": "New York",
  "person.address.state": "NY"
}

License

This project is licensed under the MIT License - see the LICENSE file for details.