@glomex/cd-json-transformation

2.1.1 • Public • Published

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 copy values.valueA to value
  • 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 );

Readme

Keywords

none

Package Sidebar

Install

npm i @glomex/cd-json-transformation

Weekly Downloads

8

Version

2.1.1

License

Unlicensed

Unpacked Size

42.6 kB

Total Files

22

Last publish

Collaborators

  • dvselas
  • klipstein
  • maxbanton
  • okinash
  • hrb-d
  • xazzzi
  • nerdbeere
  • diablero13