Define YAML Schema for JSON Arrays
Learn how to convert JSON arrays into YAML Schema definitions with items constraints. Covers typed arrays, arrays of objects, tuple validation, minItems/maxItems, and uniqueItems.
Detailed Explanation
JSON Arrays to YAML Schema
JSON arrays map to type: array in YAML Schema with an items keyword that describes the element type. The converter inspects array elements to infer the correct item schema.
Primitive Array
{
"tags": ["api", "production", "v2"],
"ports": [80, 443, 8080]
}
type: object
properties:
tags:
type: array
items:
type: string
ports:
type: array
items:
type: integer
Array of Objects
{
"users": [
{ "id": 1, "name": "Alice", "role": "admin" },
{ "id": 2, "name": "Bob", "role": "viewer" }
]
}
type: object
properties:
users:
type: array
items:
type: object
properties:
id:
type: integer
name:
type: string
role:
type: string
required:
- id
- name
- role
Array Constraints
YAML Schema supports several array-specific constraints:
tags:
type: array
items:
type: string
minItems: 1
maxItems: 10
uniqueItems: true
| Constraint | Purpose |
|---|---|
minItems |
Minimum number of elements |
maxItems |
Maximum number of elements |
uniqueItems |
All elements must be distinct |
Tuple Validation
For fixed-length arrays where each position has a different type:
coordinates:
type: array
items:
- type: number
description: Latitude
- type: number
description: Longitude
minItems: 2
maxItems: 2
Empty Arrays
An empty JSON array ([]) is ambiguous -- the converter cannot determine the item type. Most converters default to items: {} (accepts anything). You should manually set the correct item type after generation.
Nested Arrays
Arrays of arrays ([[1,2],[3,4]]) produce:
matrix:
type: array
items:
type: array
items:
type: integer
This pattern appears in matrix data, batch operations, and grid configurations.
Use Case
Configuration files for load balancers, firewall rules, and CI pipeline steps use arrays of objects extensively. A YAML schema with proper array item definitions ensures that every element in the list matches the expected shape, catching malformed entries before they cause runtime failures.