JavaScript JSON Pointer
JavaScript implementation of JSON Pointer (RFC 6901).
Can be used as Node.js package, AMD module or a plain script.
Installation
via npm:
$ npm install jsonpointer.js
via Bower:
$ bower install jsonpointer.js
or copy src/jsonpointer.js
file from repo.
Include
Node.js
var jsonpoiner = ; // XXX: '.js' is part of package name!console; // 'object'
AMD
;
<script> tag
API
jsonpointer.get
get()
accepts strings, objects and arrays as evaluation target.
var target = foo: bar: 'foobar' baz: true false '~': 'tilde' '/': 'slash'); var targetJSON = JSON; jsonpointer; // {bar: 'foobar'}jsonpointer; // 'foobar'jsonpointer; // undefinedjsonpointer; // 'tilde'jsonpointer; // 'slash'jsonpointer; // [true, false]jsonpointer; // truejsonpointer; // undefined
Second argument might be omitted, in such case get()
returns a function
that takes pointer as argument and evaluates it.
var evaluate = jsonpointer;; // 'foobar'
There are several cases when .get()
throws an exception:
- First argument is not string, object or array.
- First argument is string but is not valid JSON string.
- Seconds argument is not valid JSON Pointer string.
- Unacceptable token met during evaluation (check section 4 of spec for examples).
'-'
token used in JSON Pointer string and it's going to be evaluated inArray
context.