0.1.2 • Published 7 years ago
json-keypath v0.1.2
json-keypath
Get and Set values in JSON objects using keypaths
Install
npm install --save json-keypathUsage
Older Versions:
var JSONKeyPath = require('json-keypath');ES6 and above:
Directly import specific method with object spread
import { setValue, getValue } from 'json-keypath';Example
var JSONKeyPath = require('json-keypath');
var data = {
example: {
app: {
count: 5
}
}
}
var value = JSONKeyPath.getValue(data, 'example.app.count');
console.log(value); // 5
JSONKeyPath.setValue(data, 'example.app.count', 6);
value = JSONKeyPath.getValue(data, 'example.app.count');
console.log(value); // 6You can set values by creating new path too.
JSONKeyPath.setValue(data, 'example.app.label', 'likes');
value = JSONKeyPath.getValue(data, 'example.app.label');
console.log(value); // likesTo avoid creating new paths, pass true as the last parameter
JSONKeyPath.setValue(data, 'example.app.label', 'likes', true);
value = JSONKeyPath.getValue(data, 'example.app.label');
// error Invalid pathBy default, passing an invalid path to get would return undefined. To follow strict path, pass the last parameter as true
var value = JSONKeyPath.getValue(data, 'example.app.views');
console.log(value); // undefined
var value = JSONKeyPath.getValue(data, 'example.app.views', true);
// error Invalid pathContributing
Contributions are welcome!
Fork this repo, clone it locally, Submit a pull request once you are done with your changes