Practical guide 5 min read

Formatting, Minifying, and Generating TypeScript from JSON

Use a realistic example to validate JSON syntax, format and minify a payload, generate a TypeScript interface, and understand limits around large integers and sensitive data.

When debugging an API, the usual JSON problem is not a lack of JSON knowledge. The response may be minified into one line, copied with invalid punctuation, or used to define TypeScript types from a single incomplete sample. The JSON Formatter handles the mechanical steps, while the API contract still requires engineering judgment.

Step 1: validate the syntax first

Paste this value into the “Input JSON” panel:

{"name":"DevToolbox","enabled":true,"ports":[3000,7001,]}

The trailing comma after the last array item is invalid, so the page shows the error returned by the browser’s JSON.parse() implementation instead of producing formatted output. After removing the comma, the result updates immediately:

{
  "name": "DevToolbox",
  "enabled": true,
  "ports": [3000, 7001]
}

The toolbar supports two spaces, four spaces, or a tab for indentation. The parser accepts strict JSON, so comments, single-quoted keys, undefined, trailing commas, and JavaScript expressions are rejected.

Step 2: formatting and minifying solve different problems

Formatted output is intended for reading, reviews, and debugging. Minified output removes insignificant whitespace and is useful for request parameters, environment variables, or test fixtures. Once the input is valid, the page also displays the minified result with a copy action.

Minification does not remove spaces inside strings and does not make the data more secure. It only serializes the same JavaScript value with less whitespace.

Step 3: generate TypeScript interfaces

Selecting “Generate TS Interfaces” sends the current formatted JSON to the DevToolbox API, where Cloudflare Workers AI returns TypeScript code. A possible result is:

interface Root {
  name: string;
  enabled: boolean;
  ports: number[];
}

The result only describes the provided sample. It cannot reliably determine:

  • Whether a field is absent in other responses and should be optional.
  • Whether status is any string or a finite string union.
  • Whether a numeric-looking identifier must remain a string.
  • Whether a date string should stay a string or be converted to Date in application code.

Treat generated code as a starting point, not as the authoritative server contract.

Large integers and sensitive fields need extra care

The formatter uses the browser’s native JSON.parse(). Integers larger than Number.MAX_SAFE_INTEGER can lose precision during parsing. Database IDs, order numbers, and snowflake IDs should generally be returned as strings when exact digits matter.

Formatting, validation, and minification run locally in the browser. Only the AI type-generation action sends JSON to the server. Remove tokens, cookies, email addresses, phone numbers, and internal business fields before invoking AI, or use representative mock data with the same shape.

Use JSON String Escape when embedding ordinary text inside a JSON string. Use JSON to XML when integrating with an XML-based system. Validate the original JSON before converting it so an early error does not propagate into the next format.