Patch JSON is utility and YAML-based language to make changes over JSON and produce new updated JSON.
- Introduction
- Getting started
- Usage
- Actions
- API
- References
You can use few approaches to reach element from JSON object.
{
"component": {
"menu": {
"title": "File Menu",
"items": [
{"id": "open", "title": "open"},
{"id": "close", "title": "close"}
]
}
}
}
-
Dot notation. This approach is recommended only for simple cases, that don't require any expressions. For instance:
- Getting menu title: component.menu.title
- Getting "open" item of menu: component.menu.items[0]
-
JSONPath notation. More powerfull notation that allows us to use expressions to get element:
- Getting menu title: $.component.menu.title
- Getting "open" item of menu: $.component.menu.items[?(@.id === 'open')]
Common patch structure:
component.menu.title:
- ACTION: <ACTION NAME>
[ VALUE: <VALUE> ]
- ACTION: <ACTION NAME>
component.menu:
- ACTION: <ACTION NAME>
[ VALUE: <VALUE> ]
- ACTION: <ACTION NAME>
This structure defines sequence of actions that should applied to json.
Instal json-patch into the project:
npm install patch-json --save
or
yarn add patch-json
Let's take a look at example of patch-json usage:
const fs = require('fs');
const jsonPatch = require('patch-json');
let jsonContent = fs.readFileSync('content.json');
let patch = fs.readFileSync('patch.yaml');
// Apply patch to loaded json data:
let newJson = jsonPatch.patch(jsonContent, patch);
Add new or modify exists node of the JSON object.
Parameters
- ACTION: SET
- VALUE - {any} New value
component.menu:
- ACTION: SET
VALUE:
newProperty:
prop1: value1
Remove node from JSON object
Parameters
- ACTION: UNSET
component.menu.title:
- ACTION: REMOVE
These actions can be applied only to the arrays.
Append new item to the end of an array
Parameters
- ACTION: PUSH
- VALUE - {any} New value
component:
- ACTION: PUSH
VALUE:
newProperty:
prop1: value1
Add item to the beginning of an array
Parameters
- ACTION: UNSHIFT
- VALUE - {any} New value
component:
- ACTION: UNSHIFT
VALUE:
newProperty:
prop1: value1
Insert item into array
Parameters
- ACTION: INSERT
- [AFTER] or [BEFORE] : {String} JSONPath value
- VALUE: {any} inserted value
components:
- ACTION: INSERT
AFTER: "[?(@.id === 'search')]"
VALUE:
id: 'delete'
- ACTION: INSERT
BEFORE: "[?(@.id === 'results')]"
VALUE:
id: 'users'
Delete item from array
Parameters
- ACTION: DELETE
- ITEM: {String} JSONPath value
components:
- ACTION: DELETE
ITEM: "[?(@.id === 'search')]"
patch(jsonString, yamlString) -> Object
validate(yamlString) -> Boolean
Patch JSON uses exceptions to process errors. Error object contains name property that defines error type:
- JSON_VALIDATION_ERROR
- YAML_VALIDATION_ERROR
- YAML_STRUCTURE_ERROR
- PATCH_ERROR
Also it contains message and stack properties to get additional info about error.
try {
jsonPath.patch(jsonString, yamlString)
} catch(ex) {
if (ex.name === 'JSON_VALIDATION_ERROR') {
} else if (ex.name === 'YAML_VALIDATION_ERROR') {
} else if (ex.name === 'YAML_STRUCTURE_ERROR') {
} else if (ex.name === 'PATCH_ERROR') {
}
}
- JSONPath syntax description