json-safety
它能做什么
这个技能检查从 API、浏览器存储或用户那里读取 JSON 的代码,确保解析错误得到处理。它面向处理外部数据的开发者。
适用场景
- ✓应用在收到格式错误的 API 响应后崩溃的时候。
- ✓浏览器存储的值损坏,导致页面出错的时候。
- ✓你的代码在未检查数据结构的情况下就使用解析后的数据的时候。
技能链接
https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/json-safety/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我把 JSON 解析写得安全,以防数据格式错误吗?”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会找出 JSON.parse 调用,检查每个调用是否都包裹了错误处理,并在使用结果前验证其结构。对于外部数据,它可以建议使用数据结构校验库。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Parse JSON safely with error handling
JSON.parse() on invalid input throws a SyntaxError that will crash your application if uncaught. API responses, localStorage values, and user-provided data can all be malformed. Safe parsing with validation catches these errors at the point of parsing, not deep in your application logic when a missing property causes an unexpected error.
Quick Reference
- JSON.parse() throws a SyntaxError on invalid input — always use try/catch
- Validate the shape of parsed data before accessing its properties
- JSON.stringify() can return undefined for values that can't be serialized
- Use a type-safe parser (Zod, Valibot) for data from external sources
Check
Find all JSON.parse() calls in this file and check whether each one is wrapped in try/catch and whether the result is validated before use.
Fix
Add try/catch error handling to all JSON.parse() calls and add shape validation to protect against unexpected data structures.
Explain
Explain why JSON.parse can throw, what safe JSON parsing looks like, and when to use schema validation libraries.
Code Review
Review scripts, client components, and browser execution paths related to Parse JSON safely with error handling. Flag exact imports, event handlers, runtime side effects, or blocking operations that violate the rule, and state how the change should be verified in the browser.
For full implementation details, code examples, and framework-specific guidance,
see references/rule.md.
Rule page: https://frontendchecklist.io/rules/javascript/json-safety
常见问题
JSON 解析失败会怎样?
这个技能说,JSON.parse 会抛出语法错误,如果没有被捕获,就会导致应用崩溃。
为什么要验证解析后的数据?
这个技能说,这样可以在解析的那一刻发现意外的结构,而不是等到之后才出错。
文中提到了哪些库?
这个技能提到 Zod 和 Valibot,它们是用于外部数据的类型安全校验器。
详情
作者的原始描述
Use when reviewing scripts, client components, bundles, or runtime behavior related to Parse JSON safely with error handling. Inspect both source code and the browser execution path so fixes target the real bottleneck or bug.
热度
- GitHub 星标
- 75k
- 排名
- #2907
- 许可证
- MIT
- 更新时间
- 2026/10/31
评论
- …
同一仓库的更多技能
frontendchecklist
75k使用命令行工具,依据 380 多条关于无障碍、速度、SEO 和安全的规则,检查前端代码和线上页面。
frontend-checklist-global
75k依据全部 386 条 Front-End Checklist 规则审查前端项目,并为发现的每个问题给出具体修复方法。
404-page
75k检查你的页面缺失提示是否提供有用的链接,并返回真正的 404 状态,帮助迷路的访客找到方向。
about-page
75k审查你的网站是否有清晰的关于页面,说明由谁运营,以建立访客和搜索引擎的信任。