jsonflat
is a lightweight Node.js library that flattens nested JSON objects to a single level using customizable delimiters.
You can install jsonflat
via npm:
npm install @kishor82/jsonflat
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);
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.
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"
}
This project is licensed under the MIT License - see the LICENSE file for details.