0.1.5 • Published 4 years ago

remap-json-response v0.1.5

Weekly downloads
7
License
ISC
Repository
-
Last release
4 years ago

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": "demo1@email.com",
                "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": "demo2@email.com",
                "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": "demo1@email.com",
            "interest": "2",
            "trackingid": "ahajRRR937cjei-i8-UHk-iey d98-LLdu8-jdt73Jha",
            "sharingid": [
                "7736736JJHIU",
                "39352JJGIU",
                "0028363OOIU"
            ]
        },
        {
            "contactid": "7393783777493783",
            "email": "demo2@email.com",
            "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"
        }
    }
}

0.1.4

4 years ago

0.1.5

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago