Tools › Build › JSON to TypeScript & PHP Class Generator
🧬

JSON to TypeScript & PHP Class Generator

Paste JSON and get TypeScript interfaces or typed PHP classes in one click.

🧬 Nested objects & lists
❔ Optional and null fields
🐘 PHP 7.4 to 8.1+
🔒 Runs in your browser
🧬

JSON to TypeScript and PHP class generator

Paste any JSON. Get TypeScript interfaces or PHP classes with the right types, nested objects, and optional fields.



How to use

  1. Paste your JSON, open a file, or load a sample.
  2. Choose TypeScript or PHP and set the options you want.
  3. Copy the code or download it. Errors in the JSON are shown with the line and column.

How the types are worked out

  • Numbers. A value written like 1.5 or 2.0 becomes float in PHP. A whole number becomes int. TypeScript uses number for both.
  • Lists of objects. Every item is merged into one type. A field that is missing from some items becomes optional. A field that is null in some items becomes nullable.
  • Mixed values. A field that holds different kinds of values becomes a union, such as string | number. PHP 8.0 and newer use a union type. PHP 7.4 has none, so the type is left out and a comment explains it.
  • Empty lists. With no items to look at, the type is unknown[] in TypeScript and array in PHP.
  • Names. Nested objects are named from their key. A key like orders gives an Order type for each item.

Using the PHP classes

Decode your JSON as an array, then build the object:

$data = json_decode($json, true);
$user = Root::fromArray($data);
echo $user->name;

If the top level of your JSON is a list, map each item: array_map(fn($row) => Item::fromArray($row), $data).

What a sample can’t tell us

Types come from the example you paste, so the tool can only describe what it sees. A field that is always present in your sample may be optional in the real API. A number that is a whole number in your sample may be a decimal later. Use a sample with several items, or paste more than one, to get a more reliable result. Review the output before you rely on it.

Frequently asked questions

How do I convert JSON to a TypeScript interface?

Paste the JSON into the box on the left and choose TypeScript. The interface appears on the right. Nested objects become their own interfaces, and lists become arrays such as string[].

Does this work for JSON from an API response?

Yes. Copy the response body from your browser’s network tab, Postman or curl and paste it in. If the response is a list, the tool builds a type for one item and a type for the list.

How are optional fields detected?

When your JSON has a list of objects, a field that is missing from some of them is marked optional (name?: string in TypeScript, nullable with a default of null in PHP). A field that is present but set to null becomes string | null.

Which PHP versions are supported?

PHP 7.4 uses typed properties. PHP 8.0 adds constructor promotion and union types. PHP 8.1 and newer can mark properties readonly. Pick the one that matches your server.

Why is a field typed as mixed or left without a type?

The sample had no value to learn from (an empty list or only null), or the field holds several kinds of values and your PHP version has no union types. The docblock above the property shows the intended type.

What happens to keys like “user-name” or “1st”?

TypeScript quotes them: "user-name": string. PHP turns them into a valid property name such as $userName, and fromArray() and toArray() still use the original key.

Is my JSON uploaded anywhere?

No. The code is generated in your browser. Your JSON never leaves your device, and the tool is free with no sign-up.