Awesome json2json
An awesome json to json mapper
Installation
npm install awesome-json2json --save
Usage
import json2json from 'awesome-json2json';
let sourceJson = { foo: { bar: { baz: 1 } } };
let template = {
new_foo: 'foo.bar.baz',
};
json2json(sourceJson, template);
Template
Template is the structure of output json, and the rule of how to map one json data to another. The syntax should look like this:
{
new_foo1: 'foo.bar.baz',
new_foo2: 'foo.not_exist_key?.bar.baz',
new_foo3: (root) => { return root.foo.bar.baz; },
new_foo4: {
$path: 'foo',
$formatting: (foo) => { return foo.bar.baz; }
},
new_foo5: {
$path: 'foo',
new_bar1: 'bar.baz',
new_bar2: '$root.foo.bar.baz',
new_bar3: {
$formatting: (foo) => { return foo.bar.baz; }
},
new_bar4: {
$disable: (foo) => { return foo.bar.baz === 1; }
new_baz: 'foo.bar.baz'
},
},
new_foo_array1: 'foo_array[].bar',
new_foo_array2: {
$path: 'foo_array[]',
$formatting: (foo_item) => { return foo_item.bar; }
}
new_foo_array3: {
$path: 'foo_array[]',
new_bar: {
$path: 'bar',
$formatting: (barValue, { $item: fooItem }) => barValue + fooItem.bar
}
}
}
Features
Optional chaining
Optional chaining is a stage-1 ECMAScript feature, by adding a ?
to the end of one pathItem, it will return undefined
if the value is undefined
. As a comparison, it will throw an error without optional chaining question mark.
json2json(
{ foo: { bar: { baz: 1 } } },
{
new_foo: 'foo.not_exist_key?.bar.baz',
},
);
Function template
By passing a function to the template, the field value will be the return value of the funtion. The first argument of the function is the root of current input json.
json2json(
{ foo: { bar: { baz: 1 } } },
{
new_foo: (root) => {
return root.foo.bar.baz + '_formatted';
},
},
);
Template with $path and $formatting
We can combine the above two type of templates into one template item by passing an object to it, with key $path
and $formatting
, in this case, the first argument of $formatting
is the json result which gets from $path
.
json2json(
{ foo: { bar: { baz: 1 } } },
{
new_foo: {
$path: 'foo.bar',
$formatting: (bar) => {
return bar.baz + '_formatted';
},
},
},
);
Nested template
If we pass some keys that are not starts with $
, then it will return the new structure that contains the keys we pass.
json2json(
{ foo: { bar: { baz: 1 } } },
{
new_foo: {
$path: 'foo',
new_bar: 'bar.baz',
},
},
);
Nested template with $path and $formatting
$path
, $formatting
and nested template can work togather!
json2json(
{ foo: { bar: { baz: 1 } } },
{
new_foo: {
$path: 'foo',
$formatting: (foo) => {
return {
baz2: foo.bar.baz + '_formatted',
};
},
new_bar: 'baz2',
},
},
);
Nested template with $disable
With $disable
keyword, we can disable a field when $disable
returns true.
json2json(
{ foo: { bar: { baz: 1 } } },
{
new_foo: {
$path: 'foo',
new_bar1: {
$disable: (foo) => {
return foo.bar.baz === 1;
},
new_baz: 'bar.baz',
},
new_bar2: 'bar.baz',
},
},
);
Nested template with $default
With $default
keyword, we can provide a default value when a field returns undefined
.
json2json(
{ foo: { bar: 1 } },
{
new_foo: {
$path: 'foo',
new_bar: { $path: 'bar', $default: 11 },
new_baz: { $path: 'baz', $default: 9 },
new_qux: { $path: 'qux', $default: () => 88 },
},
},
);
Template with $root
When we reaches the very bottom, it possible to use $root
to go back to the root of input json.
json2json(
{ foo: { bar: { baz: 1 } } },
{
new_foo: {
$path: 'foo',
new_bar: {
$path: 'bar',
new_baz1: 'baz',
new_baz2: '$root.foo',
},
},
},
);
Array template
Map an array is so easy.
json2json(
{
foo: [{ bar: 1 }, { bar: 2 }, { bar: 3 }],
},
{
new_foo: 'foo[].bar',
},
);
Array template with $formatting
json2json(
{
foo: [{ bar: 1 }, { bar: 2 }, { bar: 3 }],
},
{
new_foo: {
$path: 'foo[].bar',
$formatting: (barValue) => barValue + '_formatted',
},
},
);
Nested array template
json2json(
{
foo: [{ bar: 1 }, { bar: 2 }, { bar: 3 }],
},
{
new_foo: {
$path: 'foo[]',
new_bar: {
$formatting: (fooItem) => {
return fooItem.bar;
},
},
},
},
);
$item and $root inside $formatting
The second parameter of $formatting is the context of current mapping status, including $item
and $root
.
json2json(
{
foo: [{ bar: 1 }, { bar: 2 }, { bar: 3 }],
},
{
new_foo: {
$path: 'foo[]',
new_bar1: {
$path: 'bar',
$formatting: (barValue, { $item: fooItem }) => {
return barValue + '_formatted_' + fooItem.bar;
},
},
new_bar2: (fooItem, { $root }) => {
return fooItem.bar + '_formatted_' + $root.foo.length;
},
},
},
);
Template with $item
item
represents the current array item.
json2json(
{
foo: [
{ bar: { baz1: 1, baz2: 2, baz3: 3 } },
{ bar: { baz1: 1, baz2: 2, baz3: 3 } },
{ bar: { baz1: 1, baz2: 2, baz3: 3 } },
],
},
{
new_foo: {
$path: 'foo[]',
new_bar: {
$path: 'bar',
new_baz: '$item.bar.baz1',
},
},
},
);
Using only the head of an Array
If you need to work with just a the first element of an array you can use $head
in the path to access it.
json2json({ foo: [{ bar: 1 }, { bar: 2 }] }, { first_bar: 'foo.$head.bar' });
Clear all empty data
Passing clearEmpty: true
to the third parameter of json2json
will clear all empty data including undefined
, null
, empty object {}
, empty array []
, and combination of empty object and empty array such as [{}, {}, {}]
json2json(
{
foo: [{ bar: 1 }, { bar: 2 }, { bar: 3 }],
},
{
new_foo: {
new_bar1: 'foo[].bar',
new_bar2: {
$path: 'foo[]',
new_baz1: 'baz',
new_baz2: {
new_qux: 'baz',
},
},
},
},
{
clearEmpty: true,
},
);