Integrating an API usually starts the same way: copy a JSON response, write a PHP DTO for it, then write the same shape again as a TypeScript interface. It's slow, and it's easy to get nullable fields wrong.
Now, with AI this became a bit easier, but as we learn how to do agentic programming, we also try to limit the manual prompting.
To help myself, and hopefully to help you, I created a tool for this kind of 'problem'.
It runs in the browser, so the JSON isn't uploaded. And as I said in my welcome post, since I'm a big advocate for privacy, I made sure that the tool runs ONLY in the browser and that the json isn't uploaded anywhere.
And to give you a quick overview of how it works, imagine this scenario:
Your input:
{
"order_id": 1042,
"created_at": "2025-03-14T09:26:53Z",
"customer": {
"full_name": "Ana Ruiz",
"email": "ana@example.com"
},
"items": [
{
"sku": "A-1",
"qty": 2,
"price": 9.5
},
{
"sku": "B-7",
"qty": 1,
"price": 20,
"gift_note": "Happy birthday"
}
],
"coupon": null
}
The tool's PHP output:
<?php
declare(strict_types=1);
namespace App\Dto;
use Symfony\Component\Serializer\Attribute\SerializedName;
use Symfony\Component\Validator\Constraints as Assert;
final readonly class Order
{
/**
* @param list<Item> $items
*/
public function __construct(
#[SerializedName('order_id')]
#[Assert\NotNull]
public int $orderId,
#[SerializedName('created_at')]
#[Assert\NotNull]
public \DateTimeImmutable $createdAt,
#[Assert\NotNull]
#[Assert\Valid]
public Customer $customer,
#[Assert\NotNull]
#[Assert\Valid]
public array $items,
public mixed $coupon = null,
) {
}
}
final readonly class Customer
{
public function __construct(
#[SerializedName('full_name')]
#[Assert\NotNull]
public string $fullName,
#[Assert\NotNull]
public string $email,
) {
}
}
final readonly class Item
{
public function __construct(
#[Assert\NotNull]
public string $sku,
#[Assert\NotNull]
public int $qty,
#[Assert\NotNull]
public float $price,
#[SerializedName('gift_note')]
public ?string $giftNote = null,
) {
}
}
Or, a Typescript output:
export interface Order {
order_id: number;
created_at: string;
customer: Customer;
items: Item[];
coupon: null;
}
export interface Customer {
full_name: string;
email: string;
}
export interface Item {
sku: string;
qty: number;
price: number;
gift_note?: string;
}
And voila, all done.
From the command line
Plus, on top of this, if you really love your command, you can use the command line to do this.
Shipped as a dependency-free Node script, you can script it or let an AI coding assistant call it instead of writing classes by hand:
git clone https://github.com/fedoralepceska/json-to-dto
cd json-to-dto
node cli/json-to-dto.mjs --input payload.json
It prints JSON with the generated files. Same input and settings, same output.
Top comments (0)