Skip to content

alexeykuzmin/jsonpointer.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

90 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.