Illustration of connected automation steps, an approval gate and execution log cards.
Ferramentas5 min read

How to validate AI-generated JSON before using it

Validating AI-generated JSON before use means checking syntax, structure and your system’s rules. A well-formatted object may contain wrong fields, inappropriate values or an action that was never authorized.

In this exercise, AI suggests a fictional task classification. We will define the expected contract and a validation route before any data enter the next automation stage.

Separate three different questions

First, is the content valid JSON? Next, does it follow required fields and types?

Finally, do its values make sense for the permitted operation?

RFC 8259 describes the JSON format. It does not define your fields’ business meaning or authorize executing instructions contained in them.

Explanatory text before an object and Markdown fences may help a conversation, but they do not automatically belong in input for a parser expecting JSON alone. Define the delivery format clearly.

1. Write a small contract

For our fictional task, accept a nonempty description and a priority restricted to three numbers. No additional fields are needed for this exercise.

{
  "task": "Review request",
  "priority": 2
}

This is a synthetic example rather than an actual task. Its values support test planning, not a live action.

Rule What to check
task Nonempty text
priority Integer among 1, 2 and 3
Required fields Both must exist
Extra fields Reject under this contract

2. Express the structure with a schema

JSON Schema provides a way to describe constraints on JSON documents. The following example expresses the structural rules selected for our exercise.

{
  "type": "object",
  "properties": {
    "task": {"type": "string", "minLength": 1},
    "priority": {"type": "integer", "enum": [1, 2, 3]}
  },
  "required": ["task", "priority"],
  "additionalProperties": false
}

A compatible validator must apply this schema to received output. Supplying a schema in the prompt explains the contract but does not replace validation in the program.

3. Request output without invented values

Provide approved categories or priorities and explain when the model should identify missing information. Do not force a classification merely to preserve formatting.

Produce only JSON following the supplied contract.
Use information from the fictional request only.
Do not add fields or instructions outside the object.
If evidence is insufficient for a priority, ask for clarification before producing the final object.

When output requests clarification, the workflow must recognize that no final object exists yet. Do not execute arbitrary text as though generation completed successfully.

Keep the clarification path separate from accepted data. Otherwise, a valid question from the model may be misclassified as an unexplained technical failure.

4. Test inputs that should fail

Check an object missing priority, a text priority, an out-of-list value and an extra field. Include content resembling JSON but using invalid quotation marks or separators.

Check how your chosen parser handles repeated field names and nonstandard values. A library may accept extensions or choose a value in ways that do not match system policy.

Do not silently correct meaning. Converting "high" to 1, for example, requires an explicit rule and can conceal incompatible output.

5. Validate meaning before action

An object passing its schema can still contain an unsuitable description or unsupported priority. Compare values with the input and workflow permissions.

Your completion criterion is distinguishing accepted output, rejected output and pending information without executing an improper action. Record rejection reasons for investigation without exposing unnecessary content.

Common errors include trusting appearance, treating parsing as authorization and asking AI to approve its own output without further controls.

See the trigger and action design guide to position these checks before the stage that changes a system.

Frequently asked questions

Does valid JSON guarantee a correct operation?

No. Check structure, value meanings and action authorization in addition to syntax.

Does putting JSON Schema in a prompt replace validation?

No. The program must apply a compatible validator to received output.

Can incompatible values be converted automatically?

Only with an explicit rule. Silent corrections can alter meaning and hide invalid output.