Sketch TypeScript Interfaces from a Stable JSON Example

Generate a first-pass TypeScript shape from fictional JSON, then identify where the parser's inference needs human review.

By DevSphere Editorial · · 5 min read

Open JSON Parser

Treat generated types as a sketch

A TypeScript interface can make a sample API object easier to discuss with a client team, but a single JSON example cannot express every valid future value. JSON Parser has a TypeScript Interface output option that recursively infers properties from the parsed sample. It is most useful as a starting point for conversation, not as a schema generator with optionality, unions across records, or runtime validation. For this tutorial, use a fictional reading summary with one nested station and one list of tags. We will compare each inferred type with the documented contract, then identify what a second sample should test. Use synthetic values and never paste an actual API response containing private identifiers. Preserve the sample separately and treat the produced declarations as disposable until a developer reviews edge cases such as nulls, empty arrays, and fields that are optional.

{
  "station": {"code": "DEMO-3", "active": true},
  "samples": [21.4, 21.7],
  "label": null
}

Create a readable interface

Open JSON Parser, paste the example into the JSON Input editor, and confirm Valid JSON. In Output Format, choose TypeScript Interface. The output is generated immediately from the value. The root declaration is named RootObject in the current implementation, with a nested interface name inferred from the property name station. Expect station to contain code: string and active: boolean, samples to be a number array, and label to be null. Interface generation walks the current sample; it does not inspect a formal schema or multiple responses. The path query can separately confirm station.code and samples[0], but that is still value inspection rather than type checking. If Sort Keys is enabled, nested property order may change in the input used for generation; leave it off unless a consistent presentation helps compare samples. Copy the declaration only after recording which parts are observed and which parts remain assumptions.

interface Station {
  code: string;
  active: boolean;
}

interface RootObject {
  station: Station;
  samples: number[];
  label: null;
}

Challenge the inference with edge cases

A property represented as null in the only sample becomes the literal type null, not a guessed string-or-null union. An empty array becomes any[] because there are no elements from which to infer an item type. Arrays with mixed element types are rendered as a union array, but the tool does not infer a discriminated union or explain whether each alternative is actually allowed. The implementation uses property names in generated interface members without quoting unusual names, so keys with spaces or punctuation may create invalid TypeScript. Repeated identical nested objects can share a generated interface name through a deduplication map, and naming collisions deserve review. Create additional examples for absent fields, null values, empty arrays, and alternate variants, then design the type deliberately. Do not copy the sketch into production merely because it compiles; compilation does not establish that it represents every server response.

Separate static declarations from runtime checks

A TypeScript interface disappears at runtime. It cannot validate JSON received over a network unless code uses a runtime validation strategy as well. This parser does not produce a validator, JSON Schema, optional property markers, or a request client. Use an established schema or validation library if the application must reject unexpected shapes. For code review, annotate the generated declaration with the questions it raises: can label be a string, is station always present, can samples be empty, and what happens if a sample is null? Then compare those answers to a contract or source implementation. The sample values should not be mistaken for constraints; seeing active true once does not mean false is disallowed. If the interface is copied into a design note, include the sample's limitations explicitly. Keep serialized JSON and inferred declaration side by side so reviewers can trace every field back to an observed value.

Troubleshoot and verify the draft

If no interface appears, verify JSON validity and select the TypeScript Interface format rather than Formatted JSON or Tree View. A syntax error, including a trailing comma, prevents generation. If the output contains null or any[], do not treat it as a bug automatically; those types reflect the limited evidence available in the sample. Confirm the result includes station.code as string, station.active as boolean, samples as number[], and label as null, then open a second deliberately different sample to examine optionality. The tool does not merge samples, so compare drafts manually. Its declaration generator is simplified and is not a full contract-analysis implementation. For a reliable production type, test a representative fixture set with a compiler and the application's runtime parser. Clear the editor before using unrelated data, and avoid using type inference as an excuse to expose a private payload to an unapproved tool.

A practical follow-up is to write down counterexamples before adopting the interface. One sample can set label to a harmless string, another can omit label entirely, and a third can use an empty samples array. Those examples expose whether the intended declaration should mark a property optional, include null in a union, or use a more specific element type. The parser shows one generated draft at a time; it does not compare the examples or reconcile their types automatically. A developer can then hand-author a model and add runtime checks for required fields, value ranges, and unknown keys. This treats the output as evidence about one observed object rather than truth about every future response, and it gives reviewers a clearer discussion than guessing what any[] or null should mean.

Browse all tutorials