Skip to content
JavaScript implementation of JSON Pointer
JavaScript CoffeeScript
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
src Fixed issue with Rhino
test Update Gruntfile
.gitignore Rename js-json-pointer.js to jsonPointer.js
.jshintrc Add .jshintrc
.npmignore Update .npmignore
.travis.yml Do not run Travis build on Node 0.8
Gruntfile.js Update Gruntfile
LICENSE Initial commit
README.md Update README
bower.json
package.json Bump version to 0.4.0

README.md

JavaScript JSON Pointer

Build Status Dependency Status NPM Version

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 = require('jsonpointer.js');  // XXX: '.js' is part of package name!
console.log(typeof jsonpointer);  // 'object'

AMD

require('jsonpointer', function(jsonpointer) {
  console.log(typeof jsonpointer);  // 'object'
});

<script> tag

<script src="/path/to/jsonpointer.js" type="text/javascript"></script>
<script>
   console.log(typeof window.jsonpointer);  // 'object'
</script>

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.stringify(target);

jsonpointer.get(target, '/foo');  // {bar: 'foobar'}
jsonpointer.get(target, '/foo/bar');  // 'foobar'
jsonpointer.get(target, '/some/nonexisting/path');  // undefined
jsonpointer.get(target, '/~0');  // 'tilde'
jsonpointer.get(targetJSON, '/~1');  // 'slash'
jsonpointer.get(targetJSON, '/baz');  // [true, false]
jsonpointer.get(targetJSON, '/baz/0');  // true
jsonpointer.get(targetJSON, '/baz/2');  // undefined

Second argument might be omitted, in such case get() returns a function that takes pointer as argument and evaluates it.

var evaluate = jsonpointer.get(target);
evaluate('/foo/bar');  // '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 in Array context.
Something went wrong with that request. Please try again.