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

update-json-data

v1.0.6

Published

update json in an extended way

Readme

Update Json Data by Extended Json Path

Get started: npm i update-json-data

1. Updating The non-Object and non-Array

Suppose we have the original data:

originalData: {
    "a1": true,
    "a2": 2,
    "a3": "av3",
    "a4": false,
}

What we want to apply on it is:

  • Remain a1's value
  • Modify a2's value to 233
  • Modify a3's value to "av333"
  • Modify a4's value to true
  • Ignore a5 which does not exist in original's keys

To achieve this, prepare the submitted data:

submittedData: {
    "a2": 233,
    "a3": "av333",
    "a4": true,
    "a5": "a5"
}

Then we got the expected data:

expectedData: {
    "a1": true,
    "a2": 233,
    "a3": "av333",
    "a4": true
}

2. Updating The Array (simple)

Suppose we have the original data:

originalData: {
    "a1": [
        true,
        2,
        "av3",
        false,
        "av5"
    ]
}

What we want to apply on it is: (index is counted from 0)

  • Insert an element after the the 2nd element; then:
  • Remove the 2nd element which value is "av3"; then:
  • Insert an element after the final element; then:
  • Remove below elements: (neither to consider the order or to consider changing the index every time after every removal)
    • the 0th element which value is true
    • the 1st element which value is 2
    • the 4th element which value is "av5"

To achieve this, prepare the submitted data:

submittedData: {
    "+a1[2]": "onPos2AddedAValue",  // if batch wanted: "+a1[2]": [a, serials, of, values]
    "-a1[2]": "av3",  // if batch wanted: "-a1": {1: a, 3: serials, 5: of, 8: values}
    "+a1": "addedValue123",
    "-a1": {  // note: object has no order
        1: 2,
        0: true,
        4: "av5"
    }
}

Then we got the expected data:

expectedData: {
    "a1": [
        // true,  // removed
        // 2,     // removed
        // "av3", // removed
        "onPos2AddedAValue",  // added
        false,    // remained
        // "av5", // removed
        "addedValue123"  // added
    ]
}

3. Updating The Array (complicated)

Suppose we have the original data:

originalData: {
    "a1": [
        true,
        2,
        "av3",
        false,
        "av5"
    ]
}

What we want to apply on it is: (index is counted from 0)

  • Insert an element after the the 1st element; then:
  • Insert an element list after the the 2nd element; then:
  • Remove the 6th element which value is "av3"; then:
  • Insert an element after the final element; then:
  • Remove below elements: (neither to consider the order or to consider changing the index every time after every removal)
    • the 0th element which value is true
    • the 1th element which value is 2
    • the 4th element which value is 123

To achieve this, prepare the submitted data:

submittedData: {
    "+a1[1]": "onPos1AddedAValue",
    "+a1[2]": ["onPos2AddedAValue", 123, true],
    "-a1[6]": "av3",
    "+a1": "addedValue123",
    "-a1": {  // note: object has no order
        4: 123,
        0: true,
        1: 2
    }
}

Then we got the expected data:

expectedData: {
    "a1": [
        // true,  // removed
        // 2,     // removed
        "onPos1AddedAValue",  // added
        "onPos2AddedAValue",  // added
        // 123,   // added then removed
        true,     // remained
        // "av3", // removed
        false,    // remained
        "av5",    // remained
        "addedValue123"  // added
    ]
}

4. Updating via JsonPath

Suppose we have the original data:

originalData: {
    "x": {
        "y": {
            "a1": [
                true,
                2,
                "av3",
                false,
                "av5"
            ]
        }
    }
}

What we want to apply on "$.x.y.a1" is: (index is counted from 0)

  • Insert an element after the the 1st element; then:
  • Insert an element list after the the 2nd element; then:
  • Remove the 6th element which value is "av3"; then:
  • Insert an element after the final element; then:
  • Remove below elements: (neither to consider the order or to consider changing the index every time after every removal)
    • the 0th element which value is true
    • the 1th element which value is 2
    • the 4th element which value is 123

To achieve this, prepare the submitted data:

NOTE:

for key-value like "$.x.y.±a1[1]": "onPos1AddedAValue", there are alternatives as below:

  • "±$.x.y.a1[1]": "onPos1AddedAValue"

which moves the operator '+' or '-' to the very left.

submittedData: {
    "$.x.y.+a1[1]": "onPos1AddedAValue",
    "$.x.y.+a1[2]": ["onPos2AddedAValue", 123, true],
    "$.x.y.-a1[6]": "av3",
    "$.x.y.+a1": "addedValue123",
    "$.x.y.-a1": {  // note: object has no order
        4: 123,
        0: true,
        1: 2
    }
}

// OR
submittedData: {
//   operator to the very left
//   v    
    "+$.x.y.a1[1]": "onPos1AddedAValue",
    "+$.x.y.a1[2]": ["onPos2AddedAValue", 123, true],
    "-$.x.y.a1[6]": "av3",
    "+$.x.y.a1": "addedValue123",
    "-$.x.y.a1": {  // note: object has no order
        4: 123,
        0: true,
        1: 2
    }
}

Then we got the expected data:

expectedData: {
    "x": {
        "y": {
            "a1": [
                // true,  // removed
                // 2,     // removed
                // "av3", // removed
                "onPos2AddedAValue",  // added
                false,    // remained
                // "av5", // removed
                "addedValue123"  // added
            ]
        }
    }
}

5. Updating Tree Data

Suppose we have a tree defined by below data:

// T
// |——A
// |  |——a1
// |  |——a2
// |  |——a3
// |
// |——B
// |  |——b1
// |  |——b2
// |  |——b3
// |
originalData: {
    "name": "T",
    "children": [
        {
            "name": "A",
            "children": [
                {"name": "a1"},
                {"name": "a2"},
                {"name": "a3"}
            ]
        },
        {
            "name": "B",
            "children": [
                {"name": "b1"},
                {"name": "b2"},
                {"name": "b3"}
            ]
        }
    ]
}

What we want to apply on this tres is:

  • Move node B-b3 to A-a2's next

To achieve this, prepare the submitted data:

submittedData: {
    // note: currently we don't support:
    // "$.children[?(@.name=='A')].+children[?(@.name=='a2')": "$.children[?(@.name=='B')].-children[?(@.name=='b3')"
    "$.children[?(@.name=='A')].+children[1]": "$.children[?(@.name=='B')].-children[2]"
    //                          ^         ^                                ^         ^
    //                         note1     note2                            note3     note4
    // note4: remove children[2]
    // note1: and then add to after children[1]
    // note2 and note3: the last index need to be pre-calculated
}

// OR
submittedData: {
//   operator to the very left                  operator to the very left
//   v                                          v
    "+$.children[?(@.name=='A')].children[1]": "-$.children[?(@.name=='B')].children[2]"
}

Then we got the expected data:

expectedData: {
    "name": "T",
    "children": [
        {
            "name": "A",
            "children": [
                {"name": "a1"},
                {"name": "a2"},
                {"name": "a3"},
                {"name": "b3"}
            ]
        },
        {
            "name": "B",
            "children": [
                {"name": "b1"},
                {"name": "b2"}
            ]
        }
    ]
}