Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use fast-json-patch in Angular 2 applications?

I want to use the "fast-json-patch" library (https://github.com/Starcounter-Jack/JSON-Patch) in an Angular 2 application.

I have tried adding:

'fast-json-patch': 'vendor/fast-json-patch/dist/json-patch-duplex.min.js'

in the system-config.ts file under the map but it doesn't work when importing fast-json-patch

like image 533
Rocky Avatar asked Jun 19 '16 04:06

Rocky


People also ask

What RFC 6902?

RFC 6902: JavaScript Object Notation (JSON) Patch.

How does JSON Patch work?

How it works. A JSON Patch document is just a JSON file containing an array of patch operations. The patch operations supported by JSON Patch are “add”, “remove”, “replace”, “move”, “copy” and “test”. The operations are applied in order: if any of them fail then the whole patch operation should abort.

What is application JSON Patch JSON?

JSON Patch is a web standard format for describing changes in a JSON document. It is meant to be used together with HTTP Patch which allows for the modification of existing HTTP resources. The JSON Patch media type is application/json-patch+json . JSON Patch. Filename extension.

What is JSON merge patch?

A JSON merge patch document describes changes to be made to a target JSON document using a syntax that closely mimics the document being modified.


1 Answers

1) Install the package

npm install fast-json-patch --save

2) In the component, where you want to use ist, import the functions you need:

import { compare } from 'fast-json-patch';

3) To create a Patch compare the old object with the new object:

const patch = compare(oldObj, modifiedObj);

4) Print out the result:

console.log(patch);

0:{op: "replace", path: "/firmendetails/registergericht", value: "Darmstadt xx"}
1:{op: "remove", path: "/firmendetails/handelsname"}
2:{op: "remove", path: "/firmendetails/firma"}
3:{op: "remove", path: "/firmendetails/rechtsform"}
like image 167
Jojo.Lechelt Avatar answered Sep 19 '22 13:09

Jojo.Lechelt