Coder Social home page Coder Social logo

node-json-transformer's Introduction

Status

Build Status Known Vulnerabilities

json-transformer

A Node.js package that converts json from one format to another.

Usage

First, install the package using npm:

npm install json-transformer-node --save

Then, require the package and use it like this:

var nodeJsonTransformer = require('json-transformer-node');

var data = {
    x : "xx",
    y : "yy"
}

var transformation = {
    mapping : {
        item : {
            a : "x",
            b : "y"
        }
    }
}

var output = nodeJsonTransformer.transform(data, transformation);
console.log(output);

Output : 
{
    a : "xx",
    b : "yy"
}

Type of Transformations Supported

  1. Object to Object Transformation

     var data = {
         x : "xx",
         y : {
             z: "zz"
         }
     }
     
     var transformation = {
         mapping : {
             item: {
                 a : "x",
                 b : "y.z" // dot separated path
             }
         }
     }
     
     var output = nodeJsonTransformer.transform(data, transformation);
     console.log(output);
     
     Output : 
     {
         a : "xx",
         b : "zz"
     }
    
  2. Array to Array Transformation

         var data = {
             x : "xx",
             y : [{
                 z: "z1"
                 },
                 {
                   z: "z2"
                 }]
         }
         
         var transformation = {
             "mapping" :{ 
                 "item":{
                     a : "x",
                     b : [{
                         "list" : "y",
                         "item" : {
                             "c" : "z"
                         }
                     }]
                 }
             }
         }
         
         var output = nodeJsonTransformer.transform(data, transformation);
         console.log(output);
         
         Output : 
         {
             a : "xx",
             b : [{
                     "c" : "z1"
                 },{
                     "c" : "z2"
                 }]
         }
    
  3. Using Hardcoded values

         var data = {
             x : "xx",
             y : "yy"
         }
         
         var transformation = {
             "mapping" : {
                 "item":{
                     a : "x",
                     b : "y",
                     c : "$constantValue"
                 }
             }
         }
         
         var output = nodeJsonTransformer.transform(data, transformation);
         console.log(output);
         
         Output : 
         {
             a : "xx",
             b : "yy",
             c : "constantValue"
         }
    
  4. Array to Object Transformation

         var data = {
             x : {
                 x1 : [{
                         z : "23"
                     },{
                         z : "45"
                     }]
                 },
             y : "yy"
         }
         
         var transformation = {
                         "mapping" : {
                             "item":{
                                 a : {
                                     "flat" : "x.x1#eq(1)",
                                     "item": {
                                         "c" : "z"
                                     }
                                 },
                                 b : "y"
                             }
                         }
                     }
         
         var output = nodeJsonTransformer.transform(data, transformation);
         console.log(output);
         
         Output : 
         {
             a : {
                 c : "45"
             },
             b : "yy"
         }
         
         Note :  "flat" : "x.x1#eq(1)" means flat it, x.x1 is the dot separated path to the array, 
         eq(index) specifies the index to pick
    
  5. Object to Array Transformation

         var data = {
             x : {
                 x1 : "123"
             },
             y : 456,
             z : "abc"
         }
         
         var transformation = {
             "mapping" : {
                 "item":{
                    "arrayItems": { 
                         "objectify": "x",
                         "item": { 
                             "car": {
                                 "c1" : "x1",
                                 "c2" : "$abc"
                             }
                          }
                    }
                 }
             }
         }
         
         var output = nodeJsonTransformer.transform(data, transformation);
         console.log(output);
         
         Output : 
         { 
           arrayItems: [{ car: { 
                                 c1: "123", 
                                 c2: "abc" 
                                 }
                       }]
         }
    
  6. Type Conversion

         var data = {
             x : "123",
             y : 456,
             z : "abc"
         }
         
         var transformation = {
             "mapping" : {
                 "item":{
                     a : "x(NUMBER)",
                     b : "y(STRING)",
                     c : "z(UPPER)"
                 }
             }
         }
         
         var output = nodeJsonTransformer.transform(data, transformation);
         console.log(output);
         
         Output : 
         {
             a : 123,
             b : "456",
             c : "ABC"
         }
    
  7. Operations - depends etc.

         var data = {
             x : "RED",
             y : "Jude"
         }
         
         var transformation = {
             "config": {
                 "dependentMap": {
                     "RED": "stop",
                     "GREEN": "go"
                 }
             },
             "mapping" : {
                 "item":{
                     a : "x{DEPENDS}"
                 }
             }
         }
         
         var output = nodeJsonTransformer.transform(data, transformation);
         console.log(output);
         
         Output : 
         {
             a : "stop"
         }
    

License

Apache 2.0

node-json-transformer's People

Contributors

dependabot[bot] avatar sudhirmiglani avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-json-transformer's Issues

How to configure multiple objects in to a single array

Is there a way to configure multiple objects in to a single array.
Ex: below in the input mapping that need to be transformed in to single array
Input:
{
x:"a",
y:"b",
z : "c"
}

Expected out put:
{
result : [ { name: "first item", value : "x"}, {name:"second item", value:"y"} ...}]
}

Thank you

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.