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 🙏

© 2025 – Pkg Stats / Ryan Hefner

remap-json-response

v0.1.5

Published

Remap JSON payload using JSON template

Readme

remap-json-response

Reformat/remap/convert a JSON object structure using simple JSON template.

( Version 0.1.5 includes private method declarations to be compatible with ES6 supported format i.e. using underscore _ instead of ESnext supported, hash #. This was causing issues with versions of nodejs below 12.0 )

Install via npm

$ npm install remap-json-response

API

let remap = require('remap-json-response');

//Reformat the source JSON using simple template
let remapped_json = remap.remap_json_response(source_json_data, template_json);

source_json_data

Provide the JSON object, which needs to be formatted or remapped.

Sample data:
let src_json = {
    "record": {
        "count": 3,
        "id": "asdf45TasdfE9300",
        "data": [
            {
                "contactid": "3245345345345345345",
                "emailaddress1": "[email protected]",
                "sharingid": ["7736736JJHIU", "39352JJGIU", "0028363OOIU"],
                "interestes": {
                    "data": [
                        {"attr": "1", "sense": "A", "pier": "AB"},
                        {"attr": "2", "sense": "B", "pier": "CD"},
                        {"attr": "3", "sense": "C", "pier": "EF"}
                    ]
                }
            },
            {
                "contactid": "7393783777493783",
                "emailaddress1": "[email protected]",
                "sharingid": ["98764DHEYDGD", "119363OOKJJH", "88826524IO"],
                "interestes": {
                    "data": [
                        {"attr": "4", "sense": "D", "pier": "GH"},
                        {"attr": "5", "sense": "E", "pier": "IJ"},
                        {"attr": "6", "sense": "F", "pier": "KL"}
                    ]
                }
            }
        ],
        "page": {
            "next": {
                "href": "/contacts/#kdun3l038udneouyenwo847"
            },
            "previous": {
                "href": "/contacts/#JJk897JJgjybjiy-ubjiuytgvj"
            }
        },
        "trackingid": "ahajRRR937cjei-i8-UHk-iey d98-LLdu8-jdt73Jha"
    },
    "sysdate": "2019-11-22T11:23:45Z"
};

template_json

Provide the template JSON object, which will be used for remapping the values of the source_json_data to attributes defined in template.

Sample data:
let template = {
    "data": [
        {
            "contactid": "record.data[*].contactid",
            "email": "record.data[*].emailaddress1",
            "interest": "record.data[*].interestes.data[1].attr",
            "trackingid": "record.trackingid",
            "sharingid": "record.data[*].sharingid"
        }
    ],
    "count": "record.count",
    "object": {
        "recordid": "record.id",
        "createdon": "sysdate",
        "pagination": {
            "next": "record.page.next.href",
            "previous": "record.page.previous.href"
        }
    }
};

While defining template, you need to understand the JSON object to be converted.

(Let me refer the above sample source_json_data and template_json to explain)

  • In this example for template, attributes like data, count, object, contactid etc. are the new attributes, those will be present in the reformatted JSON object.

  • Values for the remap attributes, like contactid, email, count etc., are defined by the path using dot (.) notation. Use asterisk (*) to fetch all data from the array and use specific index/position value to fetch exactly single value from array.

...
{
    "data": [
        {
            "contactid": "record.data[*].contactid",
            "email": "record.data[*].emailaddress1",
            "interest": "record.data[*].interestes.data[1].attr",
            "trackingid": "record.trackingid",
            "sharingid": "record.data[*].sharingid"
        }
    ]
    ...
...

Here the template will be interpreted as

  • Build a collection "data", where each element element will match with the member JSON
  • The number of items in the collection, "data", will be maximum size of member attribute. (It may sound confusing, but hold on)
  • Get the value for "contactid" from source_json_data. The value(s) parsed using path "record.data[*].contactid". The source_json_data should have key/attribute "record" under root; which should have an array "data". Since the template specifies "record.data[*]..."; so, it will traverse through all array item(s) at source and fetch "contactid" value. Same is the case for "email".
  • However, for "interest" key/attribute, which is a collection in the source_json_data, the program will only fetch the 2nd item from source, if it is present.
  • For "trackingid" key/attribute, which is not part of collection in the source_json_data, will be extracted from "record.trackingid" and included in each "data" item while reformatting(see the sample result for clarity)
  • For "sharingid" key/attribute, which is an array in the collection in source_json_data, will be extracted as an array to each "data" item while reformatting.
  • After fetching the values for all member attributes the program will count the number of items in each individual attributes. The maximum number of item among the attributes determines the array size in the reformatted JSON object. For example, assume after extracting the values for each member attribute; "contactid" has 4 items, "email" has 4 items, "interest" has 2 items and "sharingid" has 1 item. Then the size of "data" collection in the reformatted JSON will be 4 and empty string will be set for no values.

Result / Reformatted JSON

{
    "data": [
        {
            "contactid": "3245345345345345345",
            "email": "[email protected]",
            "interest": "2",
            "trackingid": "ahajRRR937cjei-i8-UHk-iey d98-LLdu8-jdt73Jha",
            "sharingid": [
                "7736736JJHIU",
                "39352JJGIU",
                "0028363OOIU"
            ]
        },
        {
            "contactid": "7393783777493783",
            "email": "[email protected]",
            "interest": "5",
            "trackingid": "ahajRRR937cjei-i8-UHk-iey d98-LLdu8-jdt73Jha",
            "sharingid": [
                "98764DHEYDGD",
                "119363OOKJJH",
                "88826524IO"
            ]
        }
    ],
    "count": 3,
    "object": {
        "recordid": "asdf45TasdfE9300",
        "createdon": "2019-11-22T11:23:45Z",
        "pagination": {
            "next": "/contacts/#kdun3l038udneouyenwo847",
            "previous": "/contacts/#JJk897JJgjybjiy-ubjiuytgvj"
        }
    }
}