Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A JavaScript implementation of the JSON Media Type for partial modifications: http://tools.ietf.org/html/draft-pbryan-json-patch-04
branch: master

This branch is 45 commits behind bruth:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
README.md
dharmafly-jsonpatch.js
index.html
jslitmus.js
jsonpatch.coffee
jsonpatch.js
qunit.css
qunit.js
test.js

README.md

jsonpatch.js

Library to apply JSON Patches in JavaScript http://tools.ietf.org/html/draft-pbryan-json-patch-04

View tests and litmus: http://bruth.github.com/jsonpatch-js/

For convenience, jsonpatch.apply can take a patch as an object or an array of objects. The spec states the latter as being the official patch syntax.

Note: all operations are applied in-place.

Add

Patch syntax: {add: <path>, value: <value>}

// Add property, result: {foo: 'bar'}
jsonpatch.apply({}, [{add: '/foo', value: 'bar'}]);

// Add array element, result: {foo: [1, 2, 3]}
jsonpatch.apply({foo: [1, 3]}, [{add: '/foo/1', value: 2}]);

// Complex, result: {foo: [{bar: 'baz'}]}
jsonpatch.apply({foo: [{}]}, [{add: '/foo/0/bar', value: 'baz'}]);

Remove

Patch syntax: {remove: <path>}

// Remove property, result: {}
jsonpatch.apply({foo: 'bar'}, [{remove: '/foo'}]);

// Remove array element, result: {foo: [1, 3]}
jsonpatch.apply({foo: [1, 2, 3]}, [{remove: '/foo/1'}]);

// Complex, result: {foo: [{}]}
jsonpatch.apply({foo: [{bar: 'baz'}]}, [{remove: '/foo/0/bar'}]);

Replace

Patch syntax: {replace: <path>, value: <value>}

// Replace property, result: {foo: 1}
jsonpatch.apply({foo: 'bar'}, [{replace: '/foo', value: 1}]);

// Repalce array element, result: {foo: [1, 4, 3]}
jsonpatch.apply({foo: [1, 2, 3]}, [{replace: '/foo/1', value: 4}]);

// Complex, result: {foo: [{bar: 1}]}
jsonpatch.apply({foo: [{bar: 'baz'}]}, [{replace: '/foo/0/bar', value: 1}]);

Move

Patch syntax: {move: <path>, to: <path>}

// Move property, result {bar: [1, 2, 3]}
jsonpatch.apply({foo: [1, 2, 3]}, [{move: '/foo', to: '/bar'}]);

Test

Patch syntax: {test: <path>, value: <value>}

// Test property, result: true
jsonpatch.apply({foo: 'bar'}, [{test: '/foo', value: 'bar'}]
Something went wrong with that request. Please try again.