jsonify-paths

1.1.0 • Public • Published

jsonify-paths Build Status Coverage Status

Convert strings representing paths or objects with a path and optional value to a JSON object.

Install

$ npm install jsonify-paths

Usage

const jsonifyPaths = require('jsonify-paths');

From a string

jsonifyPaths.from('a/bb/ccc');
// =>
{
    "a": {
        "bb": {
            "ccc": {}
        }
    }
}

From an array of strings

jsonifyPaths.from([
    'I am/not/a number',
    'I am/a/free man!'
]);
// =>
{
    "I am": {
        "not": {
            "a number": {}
        },
        "a": {
            "free man!": {}
        }
    }
}

From an object

jsonifyPaths.from({path: 'a/bb/ccc'});
// =>
{
    "a": {
        "bb": {
            "ccc": {}
        }
    }
}

Providing value

jsonifyPaths.from({path: 'a/bb/ccc', value: 'foo'});
// =>
{
    "a": {
        "bb": {
            "ccc": "foo"
        }
    }
}

From an array of objects

jsonifyPaths.from([
    {path: 'a/bb/ccc'},
    {path: 'a/bb/d'},
    {path: 'e'},
]);
// =>
{
    "a": {
        "bb": {
            "ccc": {},
            "d": {}
        }
    },
    "e": {}
}

Providing values (optional)

jsonifyPaths.from([
    {path: 'a/bb/ccc', value: "foo"},
    {path: 'a/bb/d'},
    {path: 'e', value: false},
    {path: 'f/g', value: ""}
    {path: 'f/h', value: 123}
]);
// =>
{
    "a": {
        "bb": {
            "ccc": "foo",
            "d": {} // => default value
        }
    },
    "e": false,
    "f": {
        "g": "",
        "h": 123
    }
}

Options

Use custom delimiters (default: slash)

jsonifyPaths.from('a>bb>ccc', {delimiter: '>'});
jsonifyPaths.from('a|bb|ccc', {delimiter: '|'});
jsonifyPaths.from('a»-»bb»-»ccc', {delimiter: '»-»');

Ignore consecutive, leading and trailing delimiters (default: true)

jsonifyPaths.from('a/b/c');
jsonifyPaths.from('/a/b/c');
jsonifyPaths.from('/a/b/c/');
jsonifyPaths.from('//a/b//c');
jsonifyPaths.from('/a /  b/ c  /');
jsonifyPaths.from('/ a //b / c///');
// =>
{
    "a": {
        "b": {
            "c": {}
        }
    }
}

Ignore spaces around delimiters (default: true)

jsonifyPaths.from({path: 'Lyon ✈ Reykjavik ✈ Vienna', value:"On Time"}, {delimiter: ''});
// =>
{
    "Lyon": {
        "Reykjavik": {
            "Vienna": "On Time"
        }
    }
}

Forcing option to false

jsonifyPaths.from('I / am not a / number', {ignoreSpacesAroundDelimiters: false});
// =>
'': {
    ' am not a ': {
        ' number': {}
    }
}

Change default value (default: {})

Default value is used when value is not set for an object

jsonifyPaths.from([
    {path: 'Lyon ✈ Berlin ✈ Rome'},
    {path: 'Lyon ✈ Berlin ✈ Geneva', value: "On Time"},
    {path: 'Bangkok ✈ Tokyo', value: "Delayed"},
], {delimiter: '', defaultValue: 'Scheduled'});
// =>
{
    "Lyon": {
        "Berlin": {
            "Rome": "Scheduled", // => default value
            "Geneva": 'On Time'
        }
    },
    "Bangkok": {
        "Tokyo": 'Delayed'
    }
};

Mix objects and strings

const res = jsonifyPaths.from([
    {path: 'Lyon ✈ Berlin ✈ Rome'},
    'Lyon ✈ Paris',
    'Bangkok ✈ Tokyo',
    {path: 'Lyon ✈ Berlin ✈ Geneva', value: 'On Time'},
], {delimiter: '', defaultValue: 'Scheduled'});
 
// =>
{
  "Lyon": {
    "Berlin": {
      "Rome": "Scheduled",
      "Geneva": "On Time"
    },
    "Paris": "Scheduled"
  },
  "Bangkok": {
    "Tokyo": "Scheduled"
  }
}

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.1.0
    0
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.1.0
    0
  • 1.0.1
    0
  • 1.0.0
    1

Package Sidebar

Install

npm i jsonify-paths

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

17.8 kB

Total Files

11

Last publish

Collaborators

  • stephanecodes