Skip to content
YAS.SH
Developer🌐 YAS server-sideAPI availableintermediate

JSON → TypeScript

Generates TypeScript interfaces or types from a JSON sample.

Processed by YAS · not stored
Ready to runInstant execution
All tools →
Loading tool…

What does this tool do?

Generates TypeScript interfaces or types from a JSON sample.

Why would I use it?

  • You are typing an API response and do not want to write the interface by hand.
  • You want to keep frontend types in sync with a backend payload.
  • You are scaffolding a typed client for a REST API.

Real-life example

Input
{"id":1,"name":"Ada","tags":["admin","dev"]}
Output
interface Root { id: number; name: string; tags: string[]; }

Property types are inferred from the sample values.

Input → Process → Output → Next

Input
Paste a representative JSON sample.
Process
YAS infers a type for every property (string, number, boolean, arrays, nested objects).
Output
A ready-to-paste TypeScript interface.
Next action
Paste it into your codebase and adjust optional/nullable fields for real payloads.

Common mistakes

  • Using a sample that misses optional fields (they will be typed as required).
  • Trusting inferred types for dynamic values like ids that can be strings or numbers.
  • Pasting the generated type without reviewing it.

What the result means

Each JSON value maps to the narrowest TypeScript type that fits the sample.

Privacy & security

Your input is sent to YAS infrastructure because the tool requires server-side processing or public network queries. Input is not stored.

API

Endpoint
POST https://yas.sh/api/v1/tools/json-to-ts
Request Header
Content-Type: application/json
cURL
curl -X POST "https://yas.sh/api/v1/tools/json-to-ts" \
  -H "Content-Type: application/json" \
  -d '{"input":"{\"name\":\"Jane\",\"age\":30}","name":"User"}'
JavaScript
const res = await fetch("https://yas.sh/api/v1/tools/json-to-ts", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
  "input": "{\"name\":\"Jane\",\"age\":30}",
  "name": "User"
}),
});
const data = await res.json();
Python
import requests

r = requests.post("https://yas.sh/api/v1/tools/json-to-ts", json={"input":"{\"name\":\"Jane\",\"age\":30}","name":"User"})
data = r.json()
FieldTypeRequiredDescription
inputstringYesJSON object
namestringNo (default Root)Interface name
Success response
{ "result": "interface User {\n  name: string;\n  age: number;\n}", "interfaceName": "User" }

Generate TypeScript interfaces from a JSON object.

Error responses
  • 400 VALIDATION_ERROR — invalid input or unsupported option.
  • 413 PAYLOAD_TOO_LARGE — input exceeds the 64 KB limit.
  • 429 RATE_LIMIT_EXCEEDED — rate limit exceeded (60 req/min).
Limits
  • Maximum input: 64 KB per request.
  • Rate limit: 60 requests/min per IP address.
  • Authenticated accounts benefit from higher tier quotas.

JSON → TypeScript: technical reference, use cases and FAQ

How JSON → TypeScript works

The sample JSON is parsed and each value inspected to infer a type: strings become string, numbers become number, booleans boolean, null becomes null, objects become nested interfaces, and arrays become the union of their element types. Nested objects are hoisted into named interfaces so the output is readable rather than a single deeply inlined structure.

Inference from one sample is inherently incomplete, and knowing why matters more than the output itself. A field absent from the sample cannot be marked optional; a field that is null in the sample but a string elsewhere infers as null; and an empty array gives no element type at all. The generated interface describes the example, not the contract — treat it as a starting point you then reconcile against the API documentation.

When to use it: real-world scenarios

Typing a third-party API with no published types

Capture a real response, generate the interface, then correct optionality and unions against the documentation. Far faster than writing it by hand.

Creating types from a fixture file

Test fixtures and seed data are already representative samples; generating types from them keeps tests and implementation aligned.

Migrating JavaScript to TypeScript

Existing API responses give the shapes you need to declare, which is usually the largest single chunk of a migration.

Documenting an undocumented internal endpoint

A generated interface is a concrete, reviewable artefact that colleagues can correct, unlike a verbal description.

Pro tips

  • Generate from the largest, most complete response you can find. Sparse samples produce types that omit half the fields.
  • Review every field for optionality by hand. Inference cannot distinguish 'always present' from 'present in this one response'.
  • Validate at the boundary with Zod or a similar runtime validator. A TypeScript interface is erased at compile time and asserts nothing about what the network actually delivers.
  • Watch for numbers that are really identifiers. A 64-bit ID inferred as number loses precision beyond 2^53−1; declare it as string.

Limitations and edge cases

What this tool deliberately does not do, and where it will disagree with other implementations.

  • Optional and nullable fields cannot be inferred from a single sample.
  • Union types are only produced where the sample itself contains mixed values.
  • Empty arrays and null-valued fields yield unusable types (never[] and null) that need manual correction.
  • Dates, enums and branded types all appear as string, because JSON has no way to express them.

Frequently asked questions

Why are none of my fields optional?
Because every field appears in the sample. Optionality is a property of the API contract, not of one response, so mark it by hand after generating.
Should I use interface or type?
Either works for object shapes. Interfaces support declaration merging and produce slightly clearer error messages; type aliases are required for unions and mapped types.
Do generated types validate my data at runtime?
No. TypeScript types are erased at compile time. Use a runtime validator such as Zod or io-ts at the network boundary if you need actual guarantees.
Why did my large ID become a number?
JSON numbers are IEEE-754 doubles, which lose precision above 2^53−1. If the API sends large IDs as numbers, type them as string in your code and be careful how you parse them.
Ask YAS AI
🍪 Cookies & privacy. Essential cookies keep you signed in and remember language and theme. Google AdSense and reCAPTCHA are Google technologies: AdSense runs only after Accept All; reCAPTCHA loads on sign-in and contact forms. See how Google uses data: https://policies.google.com/technologies/partner-sites cookie policy · privacy policy.
Settings