How to Get JSON Output From an LLM

A prompt asking for JSON is not a schema validator. When supported, use the provider’s schema-constrained output feature, then validate types, required keys and business rules in your application. Handle refusals, incomplete responses and unsupported schema features explicitly.

Published by HiddenPrompts · Updated

1 min read

Define a schema

List every key, type, allowed value, and whether it is required. Specify how missing or uncertain information must be represented.

Remove competing prose

Ask for one JSON value with no markdown fence or commentary. When supported, prefer the provider’s structured-output or schema feature over prompt-only formatting.

Validate outside the model

Parse the response, validate it against a schema, reject unexpected fields when appropriate, and retry with the validation error. Never treat generated JSON as trusted code or SQL.

Test edge cases

Include empty inputs, quotes, Unicode, long strings, ambiguous categories, and unavailable facts. Correct escaping and stable null behavior matter as much as the happy path.

Worked example

Authored teaching example, not a recorded model response.

Extract an appointment from [MESSAGE].
Required keys: date, time, timezone, missing_fields.
Use null for facts not supplied. Never guess a timezone.
Example shape (illustrative, not a model result):
{"date":"2026-10-15","time":null,"timezone":null,"missing_fields":["time","timezone"]}

How to check the result

Parse the response before consuming it. Check valid date ranges separately from JSON syntax. Reject extra fields if your contract requires that, and ensure null has a defined meaning in downstream code.

Sources and further reading

Learn the method

Testing Method · Contact

Prompt Structure Checker