@glomex/cd-json-transformation
v2.1.1
Published
## Example
Downloads
4
Keywords
Readme
cd-json-transformation
Example
Let's say we have the following existing object we want to transform:
const origin = {
id: 1,
config: {
flagA: true
},
values: {
valueA: 'foo',
valueB: 'bar'
},
arr: [0, 1, 2]
};
Now we want to do the following operations on this object:
- Keep original structure
- Remove
values
but copyvalues.valueA
tovalue
- Change
config.flagA
to false
First we use generateTemplate
to get a template based on the original object:
const { generateTemplate } = require('json-transformation');
const template = generateTemplate(origin);
The result looks like this:
const template = {
id: '{{id}}',
config: {
flagA: '{{config.flagA}}'
},
values: {
valueA: '{{values.valueA}}',
valueB: '{{values.valueB}}'
},
arr: [
'{{arr.0}}',
'{{arr.1}}',
'{{arr.2}}'
]
};
Now we make the required changes:
const template = {
id: '{{id}}',
config: {
flagA: false
},
value: '{{values.valueA}}',
arr: [
'{{arr.0}}',
'{{arr.1}}',
'{{arr.2}}'
]
};
…and use transform
to create the new object:
const { transform } = require('json-transformation');
const newObj = transform(origin, template );