새 글을 빠르게 보려면?
이 블로그 목록을 북마크하고 홈·도구 허브의 가이드 영역도 확인하세요. 글 읽기에 가입이나 메일 구독이 필요 없습니다.
object·문자열·배열·enum·anyOf·$ref 등 JSON Schema 복붙 예제와 검증 시 흔한 실수 정리. JSON Work 로컬 검증 도구와 함께 쓰기 좋은 실무 가이드입니다.
JSON Schema는 JSON 데이터의 허용 형태와 규칙을 기술합니다. 구문 검사는 “JSON이 맞는가”, 스키마 검증은 “계약에 맞는가”를 봅니다.
예시는 Draft 2020-12 ($schema) 기준입니다. 사용하는 검증기가 지원하는 초안과 맞추세요.
JSON 기초는 What is JSON?에서 복습할 수 있습니다.
name(문자열), age(정수) 필수, tags 선택:{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/schemas/person.min.json",
"title": "Person",
"type": "object",
"additionalProperties": false,
"properties": {
"name": { "type": "string", "minLength": 1 },
"age": { "type": "integer", "minimum": 0 },
"tags": {
"type": "array",
"items": { "type": "string" }
}
},
"required": ["name", "age"]
}유효 인스턴스{ "name": "Asha", "age": 30, "tags": ["beta"] }무효 예 (age 누락, additionalProperties: false 때문에 extra 불가){ "name": "Asha", "extra": true }{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties": false,
"properties": {
"handle": { "type": "string", "pattern": "^[a-z0-9_]{3,20}$" },
"contact": { "type": "string", "format": "email" },
"createdAt": { "type": "string", "format": "date-time" }
},
"required": ["handle"]
}참고: format은 코어에서 주석에 가깝고, Ajv 등은 플러그인으로 켭니다. 런타임에서 켜지 않으면 문서용으로만 취급하세요.{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties": false,
"properties": {
"qty": { "type": "integer", "minimum": 1, "maximum": 99 },
"price": { "type": "number", "minimum": 0, "multipleOf": 0.01 }
},
"required": ["qty", "price"]
}정수는 integer, 소수는 number로 구분합니다.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties": false,
"properties": {
"role": { "type": "string", "enum": ["admin", "editor", "viewer"] },
"version": { "const": 1 }
},
"required": ["role", "version"]
}{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties": false,
"properties": {
"ids": {
"type": "array",
"items": { "type": "string", "pattern": "^[A-Z]{3}-[0-9]{4}$" },
"minItems": 1,
"maxItems": 10,
"uniqueItems": true
}
},
"required": ["ids"]
}{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties": false,
"properties": {
"value": {
"anyOf": [
{ "type": "string", "minLength": 1 },
{ "type": "number" }
]
}
},
"required": ["value"]
}oneOf — 정확히 하나(겹치는 조건 주의). allOf — 모든 부분 스키마 제약 결합.{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$defs": {
"Email": { "type": "string", "format": "email" }
},
"type": "object",
"additionalProperties": false,
"properties": {
"owner": { "$ref": "#/$defs/Email" },
"billing": { "$ref": "#/$defs/Email" }
},
"required": ["owner"]
}루트에서 additionalProperties: false로 naem 같은 오타를 잡기 쉽습니다. 공개 API에서는 properties·required와 함께 쓰는 경우가 많습니다.
{ "type": ["string", "null"] } 등으로 명시.운영 파이프라인에서는 Ajv, jsonschema 등 라이브러리의 초안 지원을 확인하세요.
개발자에게 최고의 JSON 처리 도구를 제공하는 데 전념
더 많은 게시물이 곧 출시됩니다...
블로그로 돌아가기업데이트 확인 방법, 다루는 주제, 제안 방법입니다.
이 블로그 목록을 북마크하고 홈·도구 허브의 가이드 영역도 확인하세요. 글 읽기에 가입이나 메일 구독이 필요 없습니다.
JSON 검증, 포맷, 변환, 디버깅 흐름과 JSON Work 업데이트이며, 사이트의 무료 브라우저 도구와 맞물립니다.
가능합니다. About 페이지나 GitHub로 연락 주세요. 실제 연동·디버깅에 도움이 되는 주제를 우선합니다.