@dreamlake/json-layer

中文文档
0.2.0发布于 2026/10/10MITTypeScriptES Module零依赖近 30 天下载 3236

Layered JSON configuration: JSON Lines of static overwrites and operators, resolved in order, with the source line of every value.

原始使用文档
文档目录17 节
  1. Overwrite
  2. Operators
  3. insert
  4. prepend
  5. union
  6. remove
  7. delete
  8. replace
  9. merge
  10. whole value
  11. Paths
  12. Comments
  13. Nothing is reserved
  14. Errors
  15. CLI
  16. Reference
  17. License

@dreamlake/json-layer

Layer JSON changes on top of JSON. One change per line.

npm install @dreamlake/json-layer
import { resolve, explain } from '@dreamlake/json-layer';

const { value, touches } = resolve(base, [
  { id: 'provider', text: providerLayer },
  { id: 'team', text: teamLayer },
]);
explain(touches, 'network.subnets.b'); // { layer: 'team', line: 3, path: [...] }

Every example below is a base, a layer, and the result. They run as tests.

Overwrite

An object line merges objects and replaces everything else.

{"type": "t3.large", "tags": {"team": "a", "cost": "x"}, "groups": ["sg-1"], "disk": 100}
{"type": "g5.xlarge", "tags": {"team": "b"}, "groups": ["sg-2"], "disk": null}
{"type": "g5.xlarge", "tags": {"team": "b", "cost": "x"}, "groups": ["sg-2"], "disk": null}

Lines apply in order.

{}
{"a": 1}
{"a": 2, "b": 3}
{"a": 2, "b": 3}

Operators

[operator, path, ...values]

insert

{"setup": ["apt update"]}
["insert", "setup", "pip install -e .", "make"]
["insert", "env.paths", "/opt/bin"]
{"setup": ["apt update", "pip install -e .", "make"], "env": {"paths": ["/opt/bin"]}}

prepend

{"setup": ["make"]}
["prepend", "setup", "apt update"]
{"setup": ["apt update", "make"]}

union

{"groups": ["sg-1", "sg-2"]}
["union", "groups", "sg-2", "sg-3"]
{"groups": ["sg-1", "sg-2", "sg-3"]}

remove

{"groups": ["sg-1", "sg-2", "sg-1"], "mounts": [{"src": "/a", "dst": "/b"}, {"src": "/c", "dst": "/d"}]}
["remove", "groups", "sg-1"]
["remove", "mounts", {"dst": "/b", "src": "/a"}]
{"groups": ["sg-2"], "mounts": [{"src": "/c", "dst": "/d"}]}

delete

{"tags": {"team": "a", "scratch": "yes"}}
["delete", "tags.scratch"]
["delete", "tags.missing"]
{"tags": {"team": "a"}}

replace

{"tags": {"team": "a", "cost": "x"}}
["replace", "tags", {"team": "b"}]
{"tags": {"team": "b"}}

merge

{"network": {"vpc": "vpc-1", "subnets": {"a": "subnet-a", "b": "subnet-b"}}}
["merge", "network.subnets", {"b": "subnet-b2", "c": {"id": "subnet-c"}}]
{"network": {"vpc": "vpc-1", "subnets": {"a": "subnet-a", "b": "subnet-b2", "c": {"id": "subnet-c"}}}}

whole value

{"a": 1}
["replace", "", {"b": 2}]
["merge", "", {"c": 3}]
{"b": 2, "c": 3}

Paths

{}
["replace", "a.b.c", 1]
["replace", ["x.y", "z"], 2]
{"p.q": 3}
{"a": {"b": {"c": 1}}, "x.y": {"z": 2}, "p.q": 3}

Comments

{"groups": []}
// whole lines only

  // indented is fine
["insert", "groups", "sg-1"]
{"url": "https://example.com//kept"}
{"groups": ["sg-1"], "url": "https://example.com//kept"}

Nothing is reserved

{}
{"$insert": ["x"], "note": "[\"delete\", \"a\"]"}
{"$insert": ["x"], "note": "[\"delete\", \"a\"]"}

Errors

{"a": 1}
["insrt", "a", 1]
L:2: unknown operator "insrt"
{"a": 1} // no trailing comments
L:1: not JSON
["merge", "a", {
  "b": 1}]
L:1: not JSON
{"tags": "none"}
["insert", "tags", "x"]
L:1: insert needs an array at tags

CLI

json-layer resolve base.json provider.jsonl team.jsonl
json-layer resolve base.json provider.jsonl team.jsonl --explain network.subnets.b
# network.subnets.b	team.jsonl:3 (network.subnets.b)
json-layer check team.jsonl

Reference

LineEffect
{...}merge objects, replace the rest
["merge", p, {...}]the same, at p
["replace", p, v]set p to v
["delete", p]remove p if present
["insert", p, v...]append to array
["prepend", p, v...]add to array front
["union", p, v...]append values not present
["remove", p, v...]remove equal values
// ...comment, whole line
  • p: "a.b.c", or ["a.b", "c"] when a key has a dot; "" is the whole value.
  • Missing parents are created, except by delete and remove.
  • Arrays match by value, never index.
  • One document per line.
  • Comment lines make a layer non-strict JSON Lines.

License

MIT

版本与发布记录

@dreamlake/json-layer 共发布 3 个版本,首次发布于 2026/10/10;过去 12 个月发布了 3 个版本。

版本发布日期距今
0.2.0latest2026/10/101 个月前
0.1.02026/10/101 个月前
0.0.0-stage2026/10/101 个月前

关于 @dreamlake/json-layer 的常见问题

@dreamlake/json-layer 是什么?

Layered JSON configuration: JSON Lines of static overwrites and operators, resolved in order, with the source line of every value.

如何安装 @dreamlake/json-layer?

在项目目录运行 npm install @dreamlake/json-layer,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 json-layer,可以用 npx json-layer 直接运行。

@dreamlake/json-layer 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dreamlake/json-layer 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@dreamlake/json-layer 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.19。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@dreamlake/json-layer 可以商用吗?

@dreamlake/json-layer 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dreamlake/json-layer 的下载量有多少?

根据 npm 官方下载统计,@dreamlake/json-layer 在 2026/10/13 – 2026/11/11 这 30 天内被下载 3,236 次,日均约 108 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dreamlake/json-layer 还在维护吗?

最新版本 0.2.0 发布于 2026/10/10(1 个月前);至今共发布 3 个版本,其中过去 12 个月发布了 3 个;首次发布于 2026/10/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。