skeemas-json-pointer

Lightweight implementation of JSON Pointers

Usage no npm install needed!

<script type="module">
  import skeemasJsonPointer from 'https://cdn.skypack.dev/skeemas-json-pointer';
</script>

README

skeemas-json-pointer

Lightweight implementation of JSON Pointers

Install

npm install skeemas-json-pointer

Pointers for Getting Values

var jsonPointer = require('skeemas-json-pointer');

// Getting values
jsonPointer('#/foo').get({ foo:'bar' }); 
// =>'bar'

jsonPointer('#/nested/foo').get({ 
    nested: { foo:'bar' }
}); 
// =>'bar'

jsonPointer('#/nested/foo/1').get({ 
    nested: { foo:['bar','bat','baz'] }
}); 
// =>'bat'

Pointers for Setting Values

var subject = { 
    nested: { foo:'bar' },
    list: [0, 1, 2]
};

// Change a property value
jsonPointer('#/nested/foo').set('baz'); 

// Change an array item
jsonPointer('#/list/1').set('one'); 

// Append an array item
jsonPointer('#/list/-').set('last'); 

assert.deepEqual(subject, {
    nested: { foo:'baz' },
    list: [0, 'one', 2, 'last']
});