Understanding the JSON Patch
Understanding the JSON Patch
JSON Patch is a format (identified by the media type, application/ json-patch+json) for expressing changes to a target JSON document intended for use with the HTTP PATCH method to apply partial modification of the target resource. JSON Patch operations are expressed in the JSON document, as follows:
Operation | JSON Patch document |
Add: Used to add the <value> to the specified <location>. | { op: add, path: <location>, value: <value> } |
Remove: Used to remove the value at the specified <location>. | { op: remove, path: <location>} |
Replace: Used to update the given <value> at the specified <location>. | { op: replace, path: <location>, value: <value> } |
Copy: Used to copy a value from the <from-location> to the <destination-location>. | { op: copy, from: <from-location>, path: <destination-location>} |
Move: Used to move a value from the <from-location> to the <destination-location>. | { op: move, from: <from-location>, path: <destination-location>} |
Test: Used to test the presence of a given value at the specified location. The Result of the test operation can be used to decide before performing other patch operations. Processing JSON using JSON PatchThe following example reads the JSON representation of the employee object from the emp.json file and uses the javax.json.JsonPatch API to add the gender value and remove hiredate inside the JSON document:
The implementation of the previous steps is demonstrated in the following program: The preceding program reads the emp.json file and prints the input and patched JsonObject, as shown ahead: | { op: test, path: <location>, value: <value> } ] |
Comments
Post a Comment