jsonpointer
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
JSON Pointer is a Node.js implementation that allows simple addressing for JSON. It gives developers the tools they need to effortlessly extract or manipulate data within a JSON object. It achieves this by using a string of keys/indices to point to a specific location within the JSON document. The library uses the get method to extract data, and the set method for data manipulation.
To use JSON Pointer, it's important to first install it in your project using Node Package Manager (npm). Once installed, you can use the require
keyword to import JSON Pointer into your file. Here's an example:
var jsonpointer = require('jsonpointer');
var obj = { foo: 1, bar: { baz: 2}, qux: [3, 4, 5]};
jsonpointer.get(obj, '/foo'); // returns 1
jsonpointer.get(obj, '/bar/baz'); // returns 2
jsonpointer.get(obj, '/qux/0'); // returns 3
jsonpointer.get(obj, '/qux/1'); // returns 4
jsonpointer.get(obj, '/qux/2'); // returns 5
jsonpointer.get(obj, '/quo'); // returns undefined
jsonpointer.set(obj, '/foo', 6); // sets obj.foo = 6;
jsonpointer.set(obj, '/qux/-', 6) // sets obj.qux = [3, 4, 5, 6]
In this example, jsonpointer.get()
is used to return the value at the given pointer, while jsonpointer.set()
sets the value at the given pointer.
You can also compile a JSON pointer for repeated usage:
var pointer = jsonpointer.compile('/foo')
pointer.get(obj) // returns 1
pointer.set(obj, 1) // sets obj.foo = 1
JSON Pointer can be your go-to tool when you want to access and manipulate JSON data in your Node.js application.
The documentation for JSON Pointer can be found on the official JSON Pointer GitHub page at https://github.com/janl/node-jsonpointer. This includes an in-depth guide on various operations you can perform on a JSON object through JSON Pointer. Visit the GitHub page to learn more and empower your JSON management skills.