npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

patch-json

v0.0.6

Published

Apply patch to the JSON

Downloads

25

Readme

Patch JSON

Patch JSON is utility and YAML-based language to make changes over JSON and produce new updated JSON.

Contents

Introduction

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"}
                ]
            }
        }
    }
  1. 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]
  2. 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.

Getting started

Instal json-patch into the project:

npm install patch-json --save

or

yarn add patch-json

Usage

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);

Common actions

SET

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

UNSET

Remove node from JSON object

Parameters

  • ACTION: UNSET
component.menu.title:
  - ACTION: REMOVE

Array actions

These actions can be applied only to the arrays.

PUSH

Append new item to the end of an array

Parameters

  • ACTION: PUSH
  • VALUE - {any} New value
component:
    - ACTION: PUSH
      VALUE:
        newProperty:
            prop1: value1

UNSHIFT

Add item to the beginning of an array

Parameters

  • ACTION: UNSHIFT
  • VALUE - {any} New value
component:
  - ACTION: UNSHIFT
    VALUE:
      newProperty:
        prop1: value1

INSERT

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

Delete item from array

Parameters

  • ACTION: DELETE
  • ITEM: {String} JSONPath value
components:
  - ACTION: DELETE
    ITEM: "[?(@.id === 'search')]"

API

Methods

patch(jsonString, yamlString) -> Object

validate(yamlString) -> Boolean

Errors

Patch JSON uses exceptions to process errors. Error object contains name property that defines error type:

  1. JSON_VALIDATION_ERROR
  2. YAML_VALIDATION_ERROR
  3. YAML_STRUCTURE_ERROR
  4. 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') {
            
        }
    
    }

References

  1. JSONPath syntax description