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

native-json-transform

v1.0.4

Published

Restructuring and performing operations on JSON on the fly for NodeJS.

Downloads

3

Readme

native-json-transform

NPM

npm version Build Status Coverage Status Known Vulnerabilities NPM download/month NPM download total
Restructuring and performing operations on JSON on the fly for NodeJS.

Background

This is a fork version form original node-json-transform.
The difference about this with original version is that we already removed Lodash dependencies. So this library has no dependencies anymore. That is why we call it native-json-transform.

Note:

  • Code slightly different with original but all features is same.
  • Minimum requirement NodeJS 6.

Install use NPM

$ npm install native-json-transform

Usage

Basic Example

var JsonTransform = require("native-json-transform").JsonTransform;

First we need some data.

var data = {
	posts : [
		{
			title : "title1",
			description: "description1",
			blog: "This is a blog.",
			date: "11/4/2013",
			extra : {
				link : "http://goo.cm"
			},
			list1:[
				{
					name:"mike"
				}
			],
			list2:[
				{
					item: "thing"
				}
			],
			clearMe: "text"
		}
	]
};

The map defines how the output will be structured and which operations to run.

var map = {
	list : 'posts',
	item: {
		name: "title",
		info: "description",
		text: "blog",
		date: "date",
		link: "extra.link",
		item: "list1.0.name",
		clearMe: "",
		fieldGroup: ['title', 'extra']
	},
	operate: [
		{
			run: "Date.parse", on: "date"
		},
		{
			run: function(val) { return val + " more info"}, on: "info"
		}
	],
	each: function(item){
		// make changes
		item.iterated = true;
		return item;
	}
}
};

You can read this as follows:

  • Get the array of objects in "posts".
  • Map the name to title, info to description etc.
  • Run Data.parse on the date value.
  • Run each function on all items after mapping and operations.

Run it synchronously

var jsonTransform = JsonTransform(data, map);
var result = jsonTransform.make();
console.log(result);

... or asynchronously

var jsonTransform = JsonTransform(data, map);
var promise = jsonTransform.makeAsync();
promise.then((function(result){
	console.log(result);
});

The expected output.

[
	{
		name : "title1",
		info: "description1",
		text: "This is a blog.",
		date: 1383544800000,
		link: "http://goo.cm",
		info: "mike more info",
		clearMe: "",
		fieldGroup: ['title1', { link : "http://goo.cm" }],
		iterated: true
	}
]

Advanced Example

var map = {
	list: 'items',
	item: {
		id: 'id',
		sku: 'sku',
		zero: 'zero',
		toReplace: 'sku',
		errorReplace: 'notFound',
		simpleArray: ['id', 'sku','sku'],
		complexArray: [ {node: 'id'} , { otherNode:'sku' } , {toReplace:'sku'} ],
		subObject: {
			node1: 'id',
			node2: 'sku',
			subSubObject: {
				node1: 'id',
				node2: 'sku',
			}
		},
		remove: ['unwanted']
	},
	defaults: {
		"missingData": true
	},
	operate: [
		{
			run: (val) => 'replacement',
			on: 'subObject.subSubObject.node1'
		},
		{
			run: (val) => 'replacement',
			on: 'errorReplace'
		},
		{
			run: (val) => 'replacement',
			on: 'toReplace'
		},
			{
			run: (val) => 'replacement',
			on: 'simpleArray.2'
		},
		{
			run: (val) => 'replacement',
			on: 'complexArray.2.toReplace'
		}
	]
};

var object = {
	items:[
		{
			id: 'books',
			zero: 0,
			sku:'10234-12312',
			unwanted: true
		}
	]
};

var result = JsonTransform(data, map).make();

The expected output.

[
	{
	    "id": "books",
	    "sku": "10234-12312",
	    "zero": 0,
	    "toReplace": "replacement",
	    "errorReplace": "replacement",
	    "simpleArray": [
	        "books",
	        "10234-12312",
	        "replacement"
	    ],
	    "complexArray": [
	        {
	            "node": "books"
	        },
	        {
	            "otherNode": "10234-12312"
	        },
	        {
	            "toReplace": "replacement"
	        }
	    ],
	    "subObject": {
	        "node1": "books",
	        "node2": "10234-12312",
	        "subSubObject": {
	            "node1": "replacement",
	            "node2": "10234-12312"
	        }
	    },
		"missingData": true
	}
]

Multi-template Example

var data = {
    products: [{
        id: 'books0',
        zero: 0,
        sku: '00234-12312',
        subitems: [
            { subid: "0.0", subsku: "subskuvalue0.0" },
            { subid: "0.1", subsku: "subskuvalue0.1" }
        ]
    }, {
        id: 'books1',
        zero: 1,
        sku: '10234-12312',
        subitems: [
            { subid: "1.0", subsku: "subskuvalue1.0" },
            { subid: "1.1", subsku: "subskuvalue1.1" }
        ]
    }]
};

var baseMap = {
	'list': 'products',
	'item' : {
		'myid': 'id',
		'mysku': 'sku',
		'mysubitems': 'subitems'
	},
    operate: [
        {
            'run': function(ary) { 
            	return JsonTransform({list:ary}, nestedMap).make();
            }, 
            'on': 'mysubitems'
        }
    ]
};

var nestedMap = {
	'list': 'list',
	'item' : {
		'mysubid': 'subid',
		'mysubsku': 'subsku'
	}
};

var result = JsonTransform(data, baseMap).make();

The expected output.

[
	{
	    "myid": "books0",
	    "mysku": "00234-12312",
	    "mysubitems": [
	    	{ "mysubid": "0.0", "mysubsku": "subskuvalue0.0" }, 
	    	{ "mysubid": "0.1", "mysubsku": "subskuvalue0.1"}
	    ]
	}, 
	{
	    "myid": "books1",
	    "mysku": "10234-12312",
	    "mysubitems": [
	    	{ "mysubid": "1.0", "mysubsku": "subskuvalue1.0" }, 
	    	{ "mysubid": "1.1", "mysubsku": "subskuvalue1.1" }
	    ]
	}
]

Context Example

First we need some data.

    var data = {
        posts : [
            {
                title : "title1",
                description: "description1"
            }
        ]
    };

The map defines how the output will be structured and which operations to run.

    var map = {
        list : 'posts',
        item: {
            name: "title",
            info: "description"
        },
        operate: [
            {
                run: function(val, context) { return val + " more info for" + context.type},
                on: "info"
            }
        ],
        each: function(item, index, collection, context){
            // make changes
            item.type = context.type;
            return item;
        }
    };

Run it

    var jsonTransform = JsonTransform(data, map);
    var context = { type: 'my-type' };
    var result = jsonTransform.make(context);
    console.log(result);

The expected output.

    [
        {
            name : "title1",
            info: "description1 more info for my-type",
            type: 'my-type'
        }
    ]

Enjoy!

Unit Test

Unit test has been replaced from jasmine-node to mocha for better unit test.

$ npm test