npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-transformer-node

v1.0.6

Published

Json Transformer Utility. Many times you want to interact with 3rd party services or clients, this utility acts as an adaptor and help you mantain code clean by maintaining only the json mappings

Downloads

411

Readme

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