Awesome json2json
An awesome json to jsone mapper
Installation
npm install awesome-json2json --save
Usage
import json2json from 'awesome-json2json';
json2json({ foo: { bar: { baz: 1 }}}, {
new_foo: 'foo.bar.baz'
});
Features
Optional chaining
json2json({ foo: { bar: { baz: 1 }}}, {
new_foo: 'foo.not_exist_key?.bar.baz'
});
Function template
json2json({ foo: { bar: { baz: 1 }}}, {
new_foo: (root) => {
return root.foo.bar.baz + '_formatted';
}
});
Template with $path and $formatting
json2json({ foo: { bar: { baz: 1 }}}, {
new_foo: {
$path: 'foo.bar',
$formatting: (bar) => {
return bar.baz + '_formatted';
}
}
});
Template with nested template
json2json({ foo: { bar: { baz: 1 }}}, {
new_foo: {
$path: 'foo',
new_bar: 'bar.baz'
}
});
Template with nested template with $path and $formatting
json2json({ foo: { bar: { baz: 1 }}}, {
new_foo: {
$path: 'foo',
$formatting: (foo) => {
return {
baz2: foo.bar.baz + '_formatted'
}
},
new_bar: 'baz2'
}
});
Template with $root
json2json({ foo: { bar: { baz: 1 }}}, {
new_foo: {
$path: 'foo',
new_bar: {
$path: 'bar',
new_baz1: 'baz',
new_baz2: '$root.foo'
}
}
});
Array template
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'
}
});
Array template with nested template
json2json({
foo: [
{ bar: 1 },
{ bar: 2 },
{ bar: 3 }
]
}, {
new_foo: {
$path: 'foo[]',
new_bar: {
$formatting: (fooItem) => {
return fooItem.bar;
}
}
}
});