microwind/ai-skills/json-validator
Claude Code and agent skill by microwind/ai-skills
Trust tier 3
- community
- By microwind
- AI/ML
- microwind/ai-skills
- tools/json-validator/SKILL.md
- Updated 22 Sep 2026
- Version 1.0.01 version recorded
Source check pending
Checking that the SKILL.md file is still in the repository.
当验证JSON文件、检查语法、调试JSON错误或格式化JSON时,在无效JSON到达下游之前捕获错误。
Install
npx vskill@latest install microwind/ai-skills/json-validatorSKILL.md
JSON验证器技能
概述
JSON无处不在 - 配置文件、API、数据存储。无效的JSON会静默破坏系统。在它们到达生产环境之前捕获错误。
核心原则: 尽早验证,经常验证。源头的无效JSON是你代码中的错误。
何时使用
始终:
- 加载配置文件
- 解析API响应
- 提交JSON数据文件之前
- 调试解析错误时
- 验证API输入
- 检查数据完整性
触发短语:
- "验证这个JSON文件"
- "JSON语法错误"
- "检查JSON格式"
- "这个JSON有效吗?"
- "JSON解析失败"
- "格式化JSON"
JSON验证功能
语法检查
- 括号匹配验证
- 引号配对检查
- 逗号位置验证
- 转义字符检查
- Unicode字符验证
结构验证
- 数据类型检查
- 必需字段验证
- 嵌套结构检查
- 数组索引验证
- 对象键名检查
语义分析
- 业务规则验证
- 数据一致性检查
- 范围验证
- 格式验证
- 关联性检查
常见JSON错误
语法错误
问题:JSON语法不正确错误示例:{"name": "John","age": 30, ← 末尾多余的逗号"city": "New York"}解决方案:移除最后一个属性后的逗号{"name": "John","age": 30,"city": "New York"}
引号错误
问题:引号使用不正确错误示例:{name: "John", ← 键名需要引号"message": 'Hello' ← 值必须使用双引号}解决方案:所有键名和字符串值都必须使用双引号{"name": "John","message": "Hello"}
数据类型错误
问题:数据类型不匹配错误示例:{"count": "100", ← 应该是数字"active": "true", ← 应该是布尔值"items": null ← 不应该是null}解决方案:使用正确的JSON数据类型{"count": 100,"active": true,"items": []}
代码实现示例
JSON验证器
python
import jsonimport refrom typing import Dict, List, Any, Optional, Unionfrom dataclasses import dataclassfrom enum import Enumimport jsonschemaclass ValidationLevel(Enum):ERROR = "error"WARNING = "warning"INFO = "info"@dataclassclass ValidationResult:"""验证结果"""level: ValidationLevelmessage: strline: Optional[int] = Nonecolumn: Optional[int] = Nonepath: Optional[str] = Nonesuggestion: Optional[str] = None@dataclassclass JSONDocument:"""JSON文档"""content: Dict[str, Any]raw_text: strencoding: strmetadata: Dict[str, Any]class JSONValidator:"""JSON验证器"""def __init__(self):self.validation_rules = self._initialize_validation_rules()self.json_schemas = {}def validate_json_file(self, file_path: str, schema: Optional[Dict[str, Any]] = None) -> List[ValidationResult]:"""验证JSON文件"""try:with open(file_path, 'r', encoding='utf-8') as f:content = f.read()except Exception as e:return [ValidationResult(level=ValidationLevel.ERROR,message=f"无法读取文件: {str(e)}")]return self.validate_json_content(content, schema)def validate_json_content(self, content: str, schema: Optional[Dict[str, Any]] = None) -> List[ValidationResult]:"""验证JSON内容"""results = []# 语法验证syntax_results = self._validate_syntax(content)results.extend(syntax_results)# 如果有语法错误,跳过其他验证if any(r.level == ValidationLevel.ERROR for r in syntax_results):return resultstry:# 解析JSONjson_data = json.loads(content)# 结构验证structure_results = self._validate_structure(json_data)results.extend(structure_results)# 模式验证if schema:schema_results = self._validate_schema(json_data, schema)results.extend(schema_results)# 语义验证semantic_results = self._validate_semantics(json_data)results.extend(semantic_results)# 性能检查performance_results = self._check_performance(json_data)results.extend(performance_results)except json.JSONDecodeError as e:results.append(ValidationResult(level=ValidationLevel.ERROR,message=f"JSON解析错误: {str(e)}",line=getattr(e, 'lineno', None),column=getattr(e, 'colno', None)))return resultsdef _validate_syntax(self, content: str) -> List[ValidationResult]:"""验证语法"""results = []lines = content.split('\n')for line_num, line in enumerate(lines, 1):# 检查常见的语法问题# 检查末尾逗号if re.search(r',\s*[\]\}]', line):results.append(ValidationResult(level=ValidationLevel.ERROR,message="对象或数组末尾有多余的逗号",line=line_num,column=line.find(',') + 1,suggestion="移除末尾的逗号"))# 检查未引用的键名if re.search(r'["\']?\s*[a-zA-Z_][a-zA-Z0-9_]*\s*["\']?\s*:', line):match = re.search(r'["\']?\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*["\']?\s*:', line)if match and not line.strip().startswith('"'):results.append(ValidationResult(level=ValidationLevel.ERROR,message="JSON键名必须用双引号包围",line=line_num,column=line.find(match.group(1)) + 1,suggestion=f'将 {match.group(1)} 改为 "{match.group(1)}"'))# 检查单引号if "'" in line and not line.strip().startswith('//'):results.append(ValidationResult(level=ValidationLevel.ERROR,message="JSON不允许使用单引号,必须使用双引号",line=line_num,column=line.find("'") + 1,suggestion="将单引号替换为双引号"))# 检查注释if '//' in line.strip() and not line.strip().startswith('"'):results.append(ValidationResult(level=ValidationLevel.WARNING,message="标准JSON不支持注释",line=line_num,column=line.find('//') + 1,suggestion="移除注释或使用JSON5格式"))return resultsdef _validate_structure(self, json_data: Any) -> List[ValidationResult]:"""验证结构"""results = []# 检查数据类型self._check_data_types(json_data, "", results)# 检查嵌套深度max_depth = self._calculate_depth(json_data)if max_depth > 10:results.append(ValidationResult(level=ValidationLevel.WARNING,message=f"JSON嵌套层级过深 ({max_depth} 层)",suggestion="考虑扁平化数据结构"))# 检查数组大小self._check_array_sizes(json_data, "", results)# 检查对象键名self._check_object_keys(json_data, "", results)return resultsdef _check_data_types(self, data: Any, path: str, results: List[ValidationResult]):"""检查数据类型"""if isinstance(data, dict):for key, value in data.items():current_path = f"{path}.{key}" if path else key# 检查键名类型if not isinstance(key, str):results.append(ValidationResult(level=ValidationLevel.ERROR,message=f"对象键名必须是字符串,当前类型: {type(key).__name__}",path=current_path))# 递归检查值self._check_data_types(value, current_path, results)elif isinstance(data, list):for i, item in enumerate(data):current_path = f"{path}[{i}]" if path else f"[{i}]"self._check_data_types(item, current_path, results)def _calculate_depth(self, data: Any, current_depth: int = 0) -> int:"""计算嵌套深度"""if isinstance(data, dict):if not data:return current_depthreturn max(self._calculate_depth(value, current_depth + 1) for value in data.values())elif isinstance(data, list):if not data:return current_depthreturn max(self._calculate_depth(item, current_depth + 1) for item in data)else:return current_depthdef _check_array_sizes(self, data: Any, path: str, results: List[ValidationResult]):"""检查数组大小"""if isinstance(data, dict):for key, value in data.items():current_path = f"{path}.{key}" if path else keyself._check_array_sizes(value, current_path, results)elif isinstance(data, list):if len(data) > 1000:results.append(ValidationResult(level=ValidationLevel.WARNING,message=f"数组元素过多 ({len(data)} 个)",path=path,suggestion="考虑分页或限制数组大小"))for i, item in enumerate(data):current_path = f"{path}[{i}]" if path else f"[{i}]"self._check_array_sizes(item, current_path, results)def _check_object_keys(self, data: Any, path: str, results: List[ValidationResult]):"""检查对象键名"""if isinstance(data, dict):# 检查重复键名(JSON解析器会自动处理,但我们可以警告)keys = list(data.keys())unique_keys = set(keys)if len(keys) != len(unique_keys):results.append(ValidationResult(level=ValidationLevel.WARNING,message="对象中存在重复的键名",path=path,suggestion="确保键名唯一"))# 检查键名格式for key in keys:if not re.match(r'^[a-zA-Z_][a-zA-Z0-9_]*$', key):results.append(ValidationResult(level=ValidationLevel.INFO,message=f"键名 '{key}' 不符合标准命名规范",path=f"{path}.{key}" if path else key,suggestion="使用字母、数字和下划线,并以字母或下划线开头"))# 递归检查值for key, value in data.items():current_path = f"{path}.{key}" if path else keyself._check_object_keys(value, current_path, results)elif isinstance(data, list):for i, item in enumerate(data):current_path = f"{path}[{i}]" if path else f"[{i}]"self._check_object_keys(item, current_path, results)def _validate_schema(self, json_data: Any, schema: Dict[str, Any]) -> List[ValidationResult]:"""验证JSON模式"""results = []try:# 使用jsonschema库验证jsonschema.validate(json_data, schema)except jsonschema.ValidationError as e:results.append(ValidationResult(level=ValidationLevel.ERROR,message=f"模式验证失败: {e.message}",path='.'.join(str(p) for p in e.absolute_path) if e.absolute_path else None,suggestion="检查数据是否符合预期的模式"))except jsonschema.SchemaError as e:results.append(ValidationResult(level=ValidationLevel.ERROR,message=f"JSON模式错误: {e.message}",suggestion="修复JSON模式定义"))return resultsdef _validate_semantics(self, json_data: Any) -> List[ValidationResult]:"""验证语义"""results = []# 检查常见的语义问题self._check_null_values(json_data, "", results)self._check_empty_strings(json_data, "", results)self._check_numeric_ranges(json_data, "", results)self._check_date_formats(json_data, "", results)return resultsdef _check_null_values(self, data: Any, path: str, results: List[ValidationResult]):"""检查null值"""if isinstance(data, dict):for key, value in data.items():current_path = f"{path}.{key}" if path else keyif value is None:results.append(ValidationResult(level=ValidationLevel.W
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 90 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingtools/json-validator/SKILL.md
Popularity
- Repository stars
- 67
- Repository forks
- 17
- 7-day trend score
- 21
Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 15 Aug 2026, 2 months ago.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/microwind/ai-skills/json-validator)Something wrong with this skill or its page? Tell us.