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 🙏

© 2024 – Pkg Stats / Ryan Hefner

edit-json

v1.2.1

Published

Edit JSON text in-place for a minimal diff

Downloads

7

Readme

npm version downloads build status coverage status Language grade: JavaScript

edit-json

Edit a textual JSON (not a JavaScript object) for a minimal diff, either programatically or by applying a JSON Patch (RFC6902).

When serializing the result down to a string, it'll resemble the source JSON as much as possible with regards to property order, whitespace (indentation) and flow types (arrays and objects on one line).

Editing JSON is easy, just JSON.parse() and play around, then JSON.stringify(). To apply a JSON Patch, there are several packages out there.

This package focuses not on working with JSON as a JavaScript object, but as its textual representation. The package parses the JSON string (as e.g. from a file) as tokens, builds up a logical representation of it, and then applies transformations to that representation. Whitespace (tabs, spaces) as well as multi-line or single-line arrays/objects are remembered.

To do the same with YAML, check out yaml-diff-patch.

Example

Given:

{
    "x": "non-alphanumerically ordered properties, obviously",
    "foo": [ "same", "line", "array" ],
    "bar": {
        "some": "object"
    }
}

Applying the JSON Patch:

[ {
    "op": "move",
    "from": "/foo",
    "path": "/bar/herenow"
} ]

Produces:

{
    "x": "non-alphanumerically ordered properties, obviously",
    "bar": {
        "herenow": [ "same", "line", "array" ],
        "some": "object"
    }
}

Properties aren't re-ordered ("x" is still first), but by default, it will try to insert properties orderly, such as when creating "herenow" in "bar". It'll be added before "some", "h" < "s". This is done with a best effort, since it's not always possible (the object might have unordered properties).

Note also that the array is not split into multiple lines, which would happen with default JSON.stringify (unless the whole document is one line of course). The source format is kept if possible.

Install

npm i edit-json or yarn add edit-json

This is a pure ESM package, and requires Node.js >=14.13.1

Simple usage

Exports

The package exports parseJson (to be documented) and jsonPatch.

Definition

jsonPatch( json: string, operations: Operations[], options: Options ): string

Applies a list of JSON Patch operations to the source json and returns the new json string.

The options are:

  • whitespace ('auto' | 'tabs' | number): Specifies whitespace strategy. Defaults to 'auto'. Force tabs using 'tabs' or spaces using number (e.g. 2 or 4).
  • ordered (boolean): Try to insert new properties in order.

RFC6902

By the spec RFC6902, the path property of an operation (and the from in move and copy operations) must be a well-formed JSON Pointer, with encoded path segments. jsonpos exposes helpers for this.

To be more practical for programmatic usage, this package allows not only JSON Pointer strings as paths, but also arrays of (raw unencoded) strings. So you can use e.g. "/foo/bar" or ["foo", "bar"], whichever you prefer. For path segments containing e.g. a slash, it would be "/f~1o~1o/bar" or ["f/o/o", "bar"].