hashbrown

Streaming JSON Parser

A streaming JSON parser resolves useful values before the whole document arrives. Use it for a JSON source you already own: a text response, a socket, or an imported file. Choose streaming strings for fields that can display a prefix, and ordinary strings for fields that should wait for a closing quote.

Let’s preview a shopping list. The parser will receive the full JSON string so far. The preview helps the user read; a separate check of the original completed document decides whether it can be used. If you already use structured chat, consume its parsed content directly.

Preview and validation read the same source

Scroll horizontally to see the full diagram, or expand it.

Original JSONFull string so farIncremental parserResolve partial valuesLive previewMay be incompleteFinal validationSuccessful source +original JSONValidated documentApplication rules pass
A useful partial value helps the reader. Only the original completed document can pass the final application checks.
Read the diagram

The accumulated original JSON feeds two paths. The incremental parser resolves fields for a live preview. Separately, successful source completion permits parsing and validating the original full document against application rules. A resolved preview or a closed JSON root alone does not establish successful completion. The parser workbench never applies the plan.

Preview and validation read the same source

A useful partial value helps the reader. Only the original completed document can pass the final application checks.

Read the diagram

The accumulated original JSON feeds two paths. The incremental parser resolves fields for a live preview. Separately, successful source completion permits parsing and validating the original full document against application rules. A resolved preview or a closed JSON root alone does not establish successful completion. The parser workbench never applies the plan.

Preview an existing JSON source

This component does not make a model request. Its parent owns the source, passes the accumulated JSON string, and tracks successful completion separately. Create shopping-schema.ts:

import { s } from '@hashbrownai/core';

export const shoppingSchema = s.object('A shopping list', {
  title: s.string('A concise list title'),
  items: s.streaming.array('Items to buy', s.streaming.string('An item name')),
});

Then create the preview component:

import { useJsonParser } from '@hashbrownai/react';
import { shoppingSchema } from './shopping-schema';

export function ShoppingPreview({ json }: { json: string }) {
  const parser = useJsonParser(json, shoppingSchema);
  if (parser.error) return <p role="alert">Invalid shopping list.</p>;
  return (
    <section aria-label="Shopping list preview">
      <h2>{parser.value?.title ?? 'Preparing list…'}</h2>
      <ul>
        {parser.value?.items.map((item, index) => (
          <li key={index}>{item}</li>
        ))}
      </ul>
    </section>
  );
}

Pass a growing prefix such as {"title":"Groceries","items":["ap and then the full document. The title waits for its closing quote; the item can appear as a prefix. Replacing the string with a different document resets parsing. Before enabling a save action, require successful source completion and reparse the original JSON with the complete application contract. A closed root, a retained parser value, and a successful source are separate observations.

Explore it in the workbench

The workbench below replays a recorded run of this lesson.

Follow actual OpenAI JSON into framework parser values and independent final validation.

Recorded with real OpenAI JSON and actual framework parser APIs in the checked React workbench. Source completion, parser completion, original JSON validation and application actions remain separate. Application state is unchanged. Unchanged protocol events are paced at 20 ms for filming; the normal host adds no delay. Footage is slowed to half speed for readability. Replay makes no requests or writes. No narration. Read the text walkthrough.

Open full workbench
Open full workbench

Let's watch JSON become a plan. The title waits for a complete string, while step text grows as it arrives. Then we'll check the original document before calling the result complete.

Follow the JSON

  1. Open JSON parser in the canonical workbench.
  2. Send the first prompt and inspect JSON as the actual OpenAI response arrives.
  3. Switch to Parser. Compare the resolved values with source and parser completion.
  4. After the source finishes, inspect the validated original document.
  5. Ask for a shorter second step. The parser follows the new assistant response rather than appending it to the old JSON document.
  6. Replay the two turns. Chat, raw JSON, resolved values, and the bottom timeline follow the same selected event.

The replay uses real OpenAI output through the actual Hashbrown text SDK. useJsonParser receives the accumulated assistant JSON. There is no browser timer generating replacement model tokens.

Choose which values can stream

The shared Skillet schema uses an ordinary title string and streaming step strings:

Plan parser schema
const schema = s.object('A two-step plan', {
  title: s.string('A concise plan title'),
  steps: s.streaming.array(
    'Exactly two steps',
    s.streaming.string('A concise action'),
  ),
});

The title waits for its closing quote. A step can resolve to a useful string prefix before its closing quote arrives. The surrounding array can reveal values while the document is still incomplete.

These choices describe what our application can display as a preview. They do not prove the source will finish successfully. See Skillet for field resolution behavior.

Keep three completion checks separate

CheckWhat it tells us
Source lifecycleWhether the actual SDK run is generating, successfully finished, stopped, or failed.
Parser completionWhether the parser has completed its JSON root.
Original document validationWhether parsing and validating the complete original JSON succeeds, including required fields and application rules.

A JSON root can close before the source ends. A stopped source can leave useful partial values. A streaming array can withhold an invalid member, and a parser may retain its last resolved value after an error. None of these is a completed valid plan by itself.

At the final boundary, the shared helper reparses the original JSON, then checks the required title and exactly two nonempty steps. This catches missing fields and members that a preview might withhold. Errors hide stale successful-looking previews; stopped partials remain labelled stopped.

Bind the real parser

The React client derives the current assistant's raw JSON from actual SDK messages and passes it to useJsonParser with the shared schema. It captures the parser's resolved value, completion flag, and error separately from the SDK lifecycle.

The input is the full string so far. A growing prefix continues the parser session; replacing the string starts a new document. The client binds observations to the current user turn and assistant message ID, so a late value from an older request cannot become the new draft.

The standalone parser also works with a finite file, socket, or other JSON source. This workbench uses a model response so the transport, source, and parser can be inspected together. If you already use a structured chat API, consume its parsed content directly rather than parsing that typed value again.

Stop and recover

Stop aborts the actual SDK request and makes that result ineligible for application actions. A fresh request gets a new captured origin. Endpoint errors, malformed JSON, unsupported types, omitted fields, and truncated output remain visible as separate outcomes.

The parser lesson never applies a plan. Application revision stays at zero. Replay uses the stored observations; it does not feed old chunks back into the live parser, start requests, or change state.

Read the checked source

Continue with Natural language forms to copy a completed draft into editable controls and apply it explicitly.

Streaming JSON ParserPreview an existing JSON sourceExplore it in the workbenchFollow the JSONChoose which values can streamKeep three completion checks separateBind the real parserStop and recoverRead the checked source