Creating a JSON patch from two objects

Given two Javascript objects ( A and B ), there is a way to generate a JSON patch, so when this patch is applied to A , will it change the properties of the object to object B ?

For example, given the hypothetical JSONPatch function (possibly a function of a similar name with one of the following), the generate_patch function is required.

patch = generate_patch(A, B) JSONPatch.apply(patch, A) # modifies A so that it has the same properties as B.

In this question, A and B are Javascript objects. The patch created by RFC6902 is JSON, which indicates an array of operations that, when applied to A this object will become B The generate_patch function should not return JSON, although, rather, you can return a Javascript object for efficiency, which will become RFC6902 JSON-patch when JSON.stringify is called on it.

The projects that I found on the topic:

+8
json javascript json-patch
source share
3 answers

Turning my comment into an answer ...

This code https://www.npmjs.org/package/rfc6902 seems to be a complete javascript implementation of both the patch and diff for the declared RFC.

I have not used it myself, but the documentation makes it look like you requested.

+4
source share

Starting with version 0.3.9, https://github.com/Starcounter-Jack/Fast-JSON-Patch has a compare method that returns the difference between two objects. If I understand correctly, this may be what you were looking for.

+5
source share

I also wrote a library for creating patches: https://github.com/gregsexton/json-patch-gen

I learned about "rfc6902" after writing and using json-patch-gen. I'm not sure how they compare: it might be worth trying both to see if your needs are right for you.

+2
source share

All Articles