Giter Site home page Giter Site logo

json2json-transformer's Introduction

json2json-transformer

Transform a generic object using JSONPath.

Pulls data from a data object using JSONPath and generate a new objects based on a template. Each of the template's properties can pull properties from the source data or an array of all results found by its JSONPath. When pulling an array of data you can also supply a subtemplate to transform each item in the array.

Import

Import in your project using

npm i --s json2json-transformer

Usage

Import reference in javascript

const render = require('json2json-transformer');

Import reference in typescript

import * as render from 'json2json-transformer'

Define a template

let template = {
    literalProperty: "Constant",                                                                        // Constant string in template
    simpleProperty : "'$.propertyDemo1'",                                                               // Dynamic property (first level in data obj)
    composeProperty : "Two strings '$.propertyDemo1''$.propertyDemo2' and a number '$.mumberValue'",    // Computed propery thet mix strings and number
    deepProperty : "'$.complexObject.nestedProperty'",                                                  // Dynamic property (second level in data obj)
    complexObject : {                                                                                   // Complex object in template
        nestedProperty: "'$.data2' valorized with other data"                                           // This property is not present in first data obj
    },
    arrayProperty: [                                                                                    // Static array defined in template
        "literal in array",                                                                             // Static literal string in array
        "'$.stringInArray'",                                                                            // Dynamic string in array
        {                                                                                               
            ObjectInArray: "'$.inArray'"                                                                // Dynamic property inside an object in array
        }
    ],
    "'$..values'":                                                                                      // Dynamic array in template, path in data source
    ["repeatedTemplate",                                                                                // Property name in destination object
    {                                                                                                   // Array item template
        repeatedSimpleProp:"'$.val'"
    }]
}

Define object data

var data1 = {
    propertyDemo1: "some value",
    propertyDemo2: "other value",
    complexObject: {
        nestedProperty: "deep"
    },
    inArray: "Inside an object in array!",
    stringInArray: "Inside an object in array!",
    mumberValue:5000,
}

var data2 = {
    data2: "more data",
    values:[{
        val: 0
    }]
}

Apply template

// Apply data1 at template object. All un matched properties will be ignored
let step1 = render.parseTemplate(data1, template, true);

// Apply data2 at step1 output. All un matched properties will be ignored
let step2 = render.parseTemplate(data2, step1, true);

// Print output in console
console.log("template",JSON.stringify(template,null,2));
console.log("step1",JSON.stringify(step1,null,2));
console.log("step2",JSON.stringify(step2,null,2));

json2json-transformer's People

Contributors

maiorfi avatar riologiuseppe avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.