JSON Toolkit
A zero-dependency Python utility for working with JSON data. Validates, formats, minifies, queries, and inspects JSON files — all with Python's standard library.
Features
- - Pretty-print with configurable indentation (2, 4, or any number of spaces)
- Minify JSON to reduce file size for APIs and storage
- Validate JSON and get structural stats (type, key count, size)
- Query nested data with dot-notation paths including array indices
- Sort keys alphabetically for deterministic output and easier diffs
- Stdin support for use in shell pipelines with other tools
Usage Examples
Pretty-print a JSON file:
CODEBLOCK0
Validate without output:
CODEBLOCK1
Query a nested value:
CODEBLOCK2
Minify for production:
CODEBLOCK3
Sort keys for consistent diffs:
CODEBLOCK4
Pipe from curl:
CODEBLOCK5
Query Syntax
Use dot notation to navigate nested structures. Array indices are numbers:
- -
name — top-level key - INLINECODE1 — nested object key
- INLINECODE2 — first element of an array
- INLINECODE3 — field of the first array element
- INLINECODE4 — deeply nested value
Command Line Options
- -
input — JSON file path, or - for stdin - INLINECODE7 — Output file (defaults to stdout)
- INLINECODE8 — Indentation spaces (default: 2)
- INLINECODE9 — Output minified JSON (no whitespace)
- INLINECODE10 /
-q PATH — Extract a value at the given dot-notation path - INLINECODE12 — Only validate and print stats, no output
- INLINECODE13 — Sort object keys alphabetically
- INLINECODE14 — (implicit) Output is always valid JSON
技能名称: json-toolkit
详细描述:
JSON Toolkit
一个零依赖的Python工具,用于处理JSON数据。使用Python标准库即可验证、格式化、压缩、查询和检查JSON文件。
特性
- - 美化打印,支持可配置缩进(2、4或任意数量的空格)
- 压缩 JSON,减小API和存储的文件体积
- 验证 JSON并获取结构统计信息(类型、键数量、大小)
- 查询 使用点号路径(含数组索引)访问嵌套数据
- 按键排序,按字母顺序排列,实现确定性输出,便于差异比较
- 支持标准输入,可在Shell管道中与其他工具配合使用
使用示例
美化打印JSON文件:
bash
python main.py data.json
仅验证,不输出内容:
bash
python main.py config.json --validate
✓ 有效的JSON
类型:对象(12个键)
大小:4832字节
查询嵌套值:
bash
python main.py users.json --query data.users.0.name
Alice
压缩用于生产环境:
bash
python main.py config.json --minify -o config.min.json
按键排序以实现一致的差异比较:
bash
python main.py package.json --sort-keys -o package-sorted.json
从curl接收管道输入:
bash
curl -s https://api.example.com/data | python main.py - --query results.0
查询语法
使用点号导航嵌套结构。数组索引为数字:
- - name — 顶层键
- data.users — 嵌套对象键
- data.users.0 — 数组的第一个元素
- data.users.0.email — 第一个数组元素的字段
- config.servers.2.host — 深层嵌套的值
命令行选项
- - input — JSON文件路径,或-表示标准输入
- -o, --output — 输出文件(默认为标准输出)
- --indent N — 缩进空格数(默认:2)
- --minify — 输出压缩后的JSON(无空白字符)
- --query PATH / -q PATH — 提取指定点号路径下的值
- --validate — 仅验证并打印统计信息,不输出内容
- --sort-keys — 按字母顺序对对象键排序
- --json — (隐式)输出始终为有效的JSON