Skip to content

getSchema()

Infer a JSON Schema from an example document.

Signature

php
JsonFast::getSchema(string $json, ?int $output = OUTPUT_ARRAY): mixed

Parameters

NameTypeDefaultDescription
$jsonstringExample JSON document
$output?intOUTPUT_ARRAYOutput mode

Returns

Inferred schema as array, JSON string, or object depending on $output. Covers a focused subset of JSON Schema: types, required, properties, items, default — not the full draft spec.

Examples

Generate schema from production sample:

php
$sample = file_get_contents('samples/order.json');
$schema = JsonFast::getSchema($sample, JsonFast::OUTPUT_STRING);
file_put_contents('schemas/order.json', $schema);

Bootstrap validation for a new endpoint:

php
$schema = JsonFast::getSchema($firstSuccessfulResponse);
$ok = JsonFast::validateSchema($nextResponse, $schema);

Native tools, weird experiments, and practical performance work.