What is json-pointer?
The json-pointer npm package provides utilities for manipulating JSON data using JSON Pointers, which are strings that reference specific locations within a JSON document. This package allows you to get, set, and remove values in JSON objects using these pointers.
What are json-pointer's main functionalities?
Get Value
This feature allows you to retrieve a value from a JSON object using a JSON Pointer string.
const jsonPointer = require('json-pointer');
const obj = { foo: { bar: 'baz' } };
const value = jsonPointer.get(obj, '/foo/bar');
console.log(value); // Output: 'baz'
Set Value
This feature allows you to set a value in a JSON object at the location specified by a JSON Pointer string.
const jsonPointer = require('json-pointer');
const obj = { foo: { bar: 'baz' } };
jsonPointer.set(obj, '/foo/bar', 'qux');
console.log(obj); // Output: { foo: { bar: 'qux' } }
Remove Value
This feature allows you to remove a value from a JSON object at the location specified by a JSON Pointer string.
const jsonPointer = require('json-pointer');
const obj = { foo: { bar: 'baz' } };
jsonPointer.remove(obj, '/foo/bar');
console.log(obj); // Output: { foo: {} }
Other packages similar to json-pointer
jsonpath
The jsonpath package provides a way to query JSON documents using JSONPath expressions, which are similar to XPath expressions for XML. Unlike json-pointer, which uses a simpler pointer syntax, jsonpath offers more complex querying capabilities, such as filtering and recursive searches.
json-query
The json-query package allows you to query JSON objects using a simple query language. It supports nested queries, filtering, and joins. Compared to json-pointer, json-query offers more advanced querying features but may be more complex to use for simple get/set operations.
lodash
Lodash is a utility library that provides a wide range of functions for manipulating JavaScript objects, including deep property access and modification. While it is not specifically focused on JSON Pointers, it offers similar functionality through methods like _.get, _.set, and _.unset.
json-pointer
Some utilities for JSON pointers described by RFC 6901
Provides some additional stuff i needed but is not included in node-jsonpointer
Installation
node.js
$ npm install json-pointer
API
var pointer = require('json-pointer');
.get(object, pointer)
Looks up a JSON pointer in an object.
Array of reference tokens, e.g. returned by api.parse, can be passed as a pointer to .get, .set and .remove methods.
var obj = {
example: {
bla: 'hello'
}
};
pointer.get(obj, '/example/bla');
.set(object, pointer, value)
Sets a new value on object at the location described by pointer.
var obj = {};
pointer.set(obj, '/example/bla', 'hello');
.remove(object, pointer)
Removes an attribute of object referenced by pointer.
var obj = {
example: 'hello'
};
pointer.remove(obj, '/example');
.dict(object)
Creates a dictionary object (pointer -> value).
var obj = {
hello: {bla: 'example'}
};
pointer.dict(obj);
.walk(object, iterator)
Just like:
each(pointer.dict(obj), iterator);
.has(object, pointer)
Tests if an object has a value for a JSON pointer.
var obj = {
bla: 'hello'
};
pointer.has(obj, '/bla');
pointer.has(obj, '/non/existing');
.escape(str)
Escapes a reference token.
pointer.escape('hello~bla');
pointer.escape('hello/bla');
.unescape(str)
Unescape a reference token.
pointer.unescape('hello~0bla');
pointer.unescape('hello~1bla');
.parse(str)
Converts a JSON pointer into an array of reference tokens.
pointer.parse('/hello/bla');
.compile(array)
Builds a json pointer from an array of reference tokens.
pointer.compile(['hello', 'bla']);
pointer(object, [pointer, [value]])
Convenience wrapper around the api.
pointer(object)
pointer(object, pointer)
pointer(object, pointer, value)
The wrapper supports chainable object oriented style.
var obj = {anything: 'bla'};
var objPointer = pointer(obj);
objPointer.set('/example', 'bla').dict();