JSON Formatter
Format and validate JSON — pretty-print with configurable indentation or minify to a single line.
Input
- - A JSON string (object, array, string, number, boolean, or null)
- May be already formatted or a single minified line
- Optional action: format (default), minify, or validate-only
- Optional indent size: 2 (default) or 4 spaces
Output
- - format: Pretty-printed JSON with proper indentation and line breaks
- minify: Single-line compact JSON with no whitespace
- validate: A message confirming valid or invalid JSON (with error details)
Instructions
- 1. Receive the raw JSON string from the user.
- Determine the requested action (default: format).
- Attempt to parse the JSON:
- Call
JSON.parse(input) conceptually — parse the string strictly following JSON spec.
- If parsing fails, report the error with as much detail as possible (e.g., "Unexpected token at position 42", "Missing closing bracket").
- 4. For format action:
- Serialize the parsed value back to a string with the requested indent size (default: 2 spaces).
- Use
JSON.stringify(parsed, null, indentSize) semantics: keys are sorted by insertion order (not alphabetically), arrays and objects are expanded across multiple lines, strings are double-quoted.
- 5. For minify action:
- Serialize with no indentation or extra whitespace:
JSON.stringify(parsed) semantics.
- 6. For validate action:
- If parsing succeeded, report "Valid JSON" along with type and top-level key count if it is an object.
- If parsing failed, report the error message.
- 7. If sort-keys option is requested, sort all object keys alphabetically at every nesting level before serializing.
- Output the result.
Options
- -
indent: 2 (default) | 4 — number of spaces per indent level - INLINECODE6 :
format (default) | minify | INLINECODE9 - INLINECODE10 :
false (default) | true — sort object keys alphabetically
Examples
Format (default, 2-space indent)
Input:
CODEBLOCK0
Output:
CODEBLOCK1
Minify
Input:
CODEBLOCK2
Output:
CODEBLOCK3
Validate — invalid JSON
Input:
CODEBLOCK4
Output:
CODEBLOCK5
Format with 4-space indent and sort-keys
Input:
CODEBLOCK6
Output:
CODEBLOCK7
Error Handling
- - If the input is empty or whitespace-only, respond: "No input provided. Please paste a JSON string."
- If
JSON.parse fails, report the error message and the approximate position or line number if determinable. Do not attempt to silently fix the JSON — report the exact parse error. (If the user wants repair, suggest using the json-repair skill.) - If the input is valid JSON but the user asked to "format" a primitive (e.g., just
42 or "hello"), format it as-is (a primitive is valid JSON). - Never truncate the output — always return the full formatted result.
JSON 格式化工具
格式化并验证 JSON——可配置缩进的美化打印,或压缩为单行。
输入
- - JSON 字符串(对象、数组、字符串、数字、布尔值或 null)
- 可以是已格式化或单行压缩格式
- 可选操作:格式化(默认)、压缩或仅验证
- 可选缩进大小:2(默认)或 4 个空格
输出
- - 格式化:带有适当缩进和换行的美化 JSON
- 压缩:无空格的单行紧凑 JSON
- 验证:确认 JSON 有效或无效的消息(含错误详情)
操作说明
- 1. 接收用户提供的原始 JSON 字符串。
- 确定请求的操作(默认:格式化)。
- 尝试解析 JSON:
- 概念上调用 JSON.parse(input)——严格按照 JSON 规范解析字符串。
- 如果解析失败,尽可能详细地报告错误(例如:位置 42 处出现意外标记、缺少右括号)。
- 4. 对于格式化操作:
- 使用请求的缩进大小(默认:2 个空格)将解析后的值序列化回字符串。
- 使用 JSON.stringify(parsed, null, indentSize) 语义:按键的插入顺序排序(非字母顺序),数组和对象跨多行展开,字符串使用双引号。
- 5. 对于压缩操作:
- 无缩进或额外空格进行序列化:JSON.stringify(parsed) 语义。
- 6. 对于验证操作:
- 如果解析成功,报告有效 JSON以及类型,如果是对象则报告顶层键的数量。
- 如果解析失败,报告错误消息。
- 7. 如果请求了排序键选项,在序列化前对每个嵌套层级的所有对象键按字母顺序排序。
- 输出结果。
选项
- - indent:2(默认)| 4——每级缩进的空格数
- action:format(默认)| minify | validate
- sort-keys:false(默认)| true——按字母顺序排序对象键
示例
格式化(默认,2 空格缩进)
输入:
{name:Alice,age:30,hobbies:[reading,coding]}
输出:
json
{
name: Alice,
age: 30,
hobbies: [
reading,
coding
]
}
压缩
输入:
json
{
name: Alice,
age: 30
}
输出:
{name:Alice,age:30}
验证——无效 JSON
输入:
{name: Alice, age: 30}
输出:
无效 JSON:位置 1 处出现意外标记 n。键必须使用双引号字符串,字符串值必须使用双引号,而非单引号。
使用 4 空格缩进和排序键进行格式化
输入:
{z:3,a:1,m:2}
输出:
json
{
a: 1,
m: 2,
z: 3
}
错误处理
- - 如果输入为空或仅含空白字符,响应:未提供输入。请粘贴 JSON 字符串。
- 如果 JSON.parse 失败,报告错误消息以及可确定的大致位置或行号。不要尝试静默修复 JSON——报告确切的解析错误。(如果用户需要修复,建议使用 json-repair 技能。)
- 如果输入是有效 JSON 但用户要求格式化一个原始值(例如仅 42 或 hello),按原样格式化(原始值是有效 JSON)。
- 切勿截断输出——始终返回完整的格式化结果。