npm

npm’s 2019 JavaScript ecosystem survey analysis is now available! Get your copy here »

jsonremap

1.1.5 • Public • Published

JSON Remap

Flattens any JSON structure with ability to rename fields.
Handles arrays and normalizes data.
Best suitable for CSV preparing.

Install

Use NPM:

$ npm install jsonremap

or Mercurial:

$ hg clone ssh://hg@bitbucket.org/andreyh/jsonremap

Usage example

const JSONRemap = require('..');
 
let data = [{
    "name": "Name 1",
    "about": "About 1",
    "records": [
        {
            "date": "20180101",
            "sum": 100,
            "array": [
                1, 2, 3
            ],
            "count": 1
        },
        {
            "date": "20180202",
            "sum": [200, 300],
            "count": 2
        },
        {
            "array": [
                3, 4, 5
            ],
            "count": 3
        },
    ],
}]
 
let mapping = {
    "title": "name",
    "description": "about",
    "date": ["records.date", ""],
    "sum": {
        "getter": "records.sum",
        "defaultValue": 0,
    },
    "array": {
        "getter": "records.array",
        "defaultValue": [],
        "unwind": false,
    },
    "count": {
        "getter": "records.count",
        "defaultValue": 0,
        "convert": function(val) {
            return val * 3
        },
    },
    "calculateMe": {
        "calculate": function() {
            return this.count + 1;
        }
    }
}
 
let result = JSONRemap(data, mapping);
 
console.log(JSON.stringify(result, null, 4));

Result:

[
    {
        "title": "Name 1",
        "description": "About 1",
        "calculateMe": 4,
        "date": "20180101",
        "sum": 100,
        "array": [
            1,
            2,
            3
        ],
        "count": 3
    },
    {
        "title": "Name 1",
        "description": "About 1",
        "calculateMe": 7,
        "date": "20180202",
        "array": [],
        "count": 6,
        "sum": 200
    },
    {
        "title": "Name 1",
        "description": "About 1",
        "calculateMe": 7,
        "date": "20180202",
        "array": [],
        "count": 6,
        "sum": 300
    },
    {
        "title": "Name 1",
        "description": "About 1",
        "calculateMe": 10,
        "date": "",
        "sum": 0,
        "array": [
            3,
            4,
            5
        ],
        "count": 9
    }
]

Documentation

JSONRemap parameters:

  1. data

    It can be null, primitive, an object or an array

  2. mapping

    It is a key-value object The key represents a key name in the output
    The value can be:

    • String: a path for a key
    "path.to.the.value"
    • Array: First element is a key, second one is a default value
    ["path.to.the.key", "defaultValue"]
    • Object:
     {
         "getter": [String], // a path to value
         "defaultValue": [String], // default value if a key doesnt exist
         "unwind": [Boolean], // If a value if array and set to false, the value remains untouched
         "convert": [Function], // A convert function to transform a value
         "calculate": [Function], // A calculation function
     }

License

MIT (see License)

install

npm i jsonremap

Downloadsweekly downloads

42

version

1.1.5

license

MIT

repository

Gitbitbucket

last publish

collaborators

  • avatar
Report a vulnerability