
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
Patch JSON is utility and YAML-based language to make changes over JSON and produce new updated JSON.
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"}
]
}
}
}
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
component.menu:
- ACTION: SET
VALUE:
newProperty:
prop1: value1
Remove node from JSON object
Parameters
component.menu.title:
- ACTION: REMOVE
These actions can be applied only to the arrays.
Append new item to the end of an array
Parameters
component:
- ACTION: PUSH
VALUE:
newProperty:
prop1: value1
Add item to the beginning of an array
Parameters
component:
- ACTION: UNSHIFT
VALUE:
newProperty:
prop1: value1
Insert item into array
Parameters
components:
- ACTION: INSERT
AFTER: "[?(@.id === 'search')]"
VALUE:
id: 'delete'
- ACTION: INSERT
BEFORE: "[?(@.id === 'results')]"
VALUE:
id: 'users'
Delete item from array
Parameters
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:
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') {
}
}
FAQs
Apply patch to the JSON
We found that patch-json demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.