Prompt Engineering JSON Output Nested Data

Nested JSON Output — Arrays and Objects

Flat schemas are easy; reliability gets hard at the first nested array. How to contract arrays and objects so the structure survives the model.

Overview

Most JSON contract failures hide in the nesting: the array that comes back as a comma-joined string, the object that flattens into prefixed keys, the list of objects where item shapes drift between elements. Contracting nested output means showing it: the example response must demonstrate the exact nesting, and field descriptions must define what array items and object properties contain. This resource loads a user-profile schema with both an array field and an object field — the smallest schema that exercises every nesting rule.

How to use this resource

  1. Generate and study the example

    The example demonstrates the array AND the object nesting explicitly — the model imitates what it sees.

  2. Define items in descriptions

    'Topic interests, lowercase tags' defines array items; 'with street and city' defines object properties. Descriptions are the nested schema.

  3. Watch the null rule on nested fields

    An unknown address is null — not an empty object, not omitted. One convention, applied at every depth.

Why This Works

  • Demonstrated nesting in the example is the strongest structural signal a prompt can send
  • Item-level descriptions give arrays and objects the schema depth that type labels alone lack
  • Uniform null discipline at every depth keeps deserializers from special-casing nested absence

Best for

  • Schemas with tags, lists, or address-style sub-objects
  • API responses where consumers index into nested paths
  • Anyone whose arrays come back as "item1, item2" strings

Not for

  • CSV destinations — flatten first or switch formats; the builder warns you
  • Deeply recursive structures — beyond two levels, consider splitting the call

Use cases

  • Stopping array fields from collapsing into comma-joined strings
  • Keeping object fields nested instead of flattened into prefixed keys
  • Defining item shapes so lists stay homogeneous across elements

FAQ

How do I stop the interests array from coming back as a comma-joined string?

The contract fights that collapse in two spots. The example shows interests as a real bracketed list (['item-one', 'item-two']), and the schema labels it 'Topic interests, lowercase tags', so the model reads items as array elements, not one string. The JSON Output Prompt Builder writes both signals into the prompt; deep nesting still slips sometimes, so parse the array after running the prompt in ChatGPT, Claude, or Gemini.

Why does my address object flatten into prefixed keys like address_street instead of staying nested?

Flattening shows up when a prompt names sub-fields but never demonstrates the nesting. This one prevents it by rendering address as a genuine nested object in the example, street and city as inner keys, plus the label 'Postal address with street and city'. The model imitates that shape. The builder bakes the signal into the prompt; confirm the nested path survived, since keys can still get hoisted up.

When the signup input has no address, should the model return null, an empty object, or omit the key?

Null, with the key still present. One convention runs at every depth: optional fields with no value are set to null and never omitted, so an unknown address returns as "address": null, not {} or a dropped key. That uniformity lets your deserializer treat nested absence like a missing top-level field. The builder encodes the rule; still verify the object case, where an empty {} is the common slip.

How deep can I nest before this contract stops being reliable?

Two levels is the practical ceiling this schema was built to exercise: a top object holding one flat array (interests) plus one one-deep sub-object (address, with street and city). Push a deeper recursive tree through a single prompt and reliability falls off; splitting generation across calls holds up better. The builder still emits a contract for deeper shapes, so validate the deepest paths hardest wherever you run it.

More resources from JSON Output Prompt Builder

Resources that pair well

Related tools

Tip: Save time by exploring related resources and tools that integrate with this resource.