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

json2json-transformer

v1.0.0

Published

Simple library to transform json documents using json templates

Downloads

8

Readme

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
    "'$.propertyName'": "Constant 2",                                                                   // Change property name at step3
    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
        "'$.nestedPropertyName'": "'$.data2' valorized with other data"                                 // Change nested property name and value at step3
    },
    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
    {                                 
        repeatedSimpleProp:"'$.val'"                                                                    // Array item template
    }]
}

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
    }]
}

var data3 = {
    propertyName: "A property",
    nestedPropertyName: "Nested Property Name"
}

Apply template

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

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

let step3 = render.parseTemplate(data3, step2, {
    clone: true,
    transformProps: (s) => s.replace(/ /g, "-").toLowerCase()
});

// 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));
console.log("step3", JSON.stringify(step3, null, 2));