JsonUtils
This library provides utility functions for working with JSON data, including mapping and flattening JSON objects, and converting JSON data to CSV format.
Installation
npm i @pixelguild/json-utils
Usage
mapJsonKeys
import { JsonUtils } from "@pixelguild/json-utils";
const json = {
firstName: "John",
lastName: "Doe",
age: 30,
};
const keyMapping = {
firstName: "first_name",
lastName: "last_name",
};
const mappedJson = JsonUtils.mapJsonKeys(json, keyMapping);
console.log(mappedJson);
flattenJson
import { JsonUtils } from "@pixelguild/json-utils";
const json = {
name: {
first: "John",
last: "Doe",
},
age: 30,
};
const flatJson = JsonUtils.flattenJson(json);
console.log(flatJson);
jsonToCsv
import { JsonUtils } from "@pixelguild/json-utils";
const json = [
{
firstName: "John",
lastName: "Doe",
age: 30,
},
{
firstName: "Jane",
lastName: "Doe",
age: 28,
},
];
const csv = JsonUtils.jsonToCsv(json);
console.log(csv);
jsonToCsv (with options)
import { JsonUtils } from "./JsonUtils";
const json = [
{
firstName: "John",
lastName: "Doe",
age: 30,
},
{
firstName: "Jane",
lastName: "Doe",
age: 28,
},
];
const options = {
includeFields: ["firstName", "age"],
};
const csv = JsonUtils.jsonToCsv(json, options);
console.log(csv);