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

@mazhu/jsonpatch

v1.0.0

Published

Command-line JSON file processing tool with patch, diff, merge, validate, and flatten operations

Readme

@mazhu/jsonpatch

命令行 JSON 文件处理工具,支持 patch、diff、merge、validate、flatten 操作。完全实现 RFC 6902 JSON Patch 规范。

安装

npm install -g @mazhu/jsonpatch

命令

patch - 应用 JSON Patch

jsonpatch patch <file> --ops '[{"op":"add","path":"/name","value":"test"}]'

支持的 JSON Patch 操作(RFC 6902):

  • add - 在指定路径添加值
  • remove - 移除指定路径的值
  • replace - 替换指定路径的值
  • move - 从一个路径移动值到另一个路径
  • copy - 复制值到新路径
  • test - 测试路径上的值

示例:

# 添加字段
jsonpatch patch data.json --ops '[{"op":"add","path":"/name","value":"test"}]'

# 替换值
jsonpatch patch config.json --ops '[{"op":"replace","path":"/version","value":"2.0.0"}]'

# 删除字段
jsonpatch patch data.json --ops '[{"op":"remove","path":"/temp"}]'

# 多个操作
jsonpatch patch data.json --ops '[{"op":"add","path":"/name","value":"test"},{"op":"remove","path":"/temp"}]'

diff - 生成差异

生成两个 JSON 文件之间的 JSON Patch 差异:

jsonpatch diff old.json new.json

示例:

jsonpatch diff v1.json v2.json -o changes.json

merge - 深度合并

深度合并两个 JSON 对象:

jsonpatch merge base.json override.json

示例:

jsonpatch merge defaults.json config.json -o merged.json

validate - 验证 JSON

验证 JSON 文件语法:

jsonpatch validate <file>

示例:

jsonpatch validate config.json
# 输出: ✓ config.json is valid JSON

flatten - 扁平化 JSON

将嵌套 JSON 对象扁平化为点记法:

jsonpatch flatten <file>

示例:

输入:

{
  "user": {
    "name": "Alice",
    "address": {
      "city": "Beijing"
    }
  }
}

输出:

{
  "user.name": "Alice",
  "user.address.city": "Beijing"
}

选项

  • -o, --output <file> - 输出到文件(默认输出到 stdout)
  • -h, --help - 显示帮助信息
  • -v, --version - 显示版本号

路径语法

JSON Patch 使用 JSON Pointer (RFC 6901) 路径语法:

  • / - 根对象
  • /foo - 对象的 foo 属性
  • /foo/bar - 嵌套路径
  • /foo/0 - 数组索引
  • /- - 数组末尾(仅用于 add 操作)

特殊字符转义:

  • ~0 表示 ~
  • ~1 表示 /

许可证

MIT

作者

Mike Wang (迈克王)