PHP to JSON Converter
Paste PHP classes, arrays, or objects. Get clean JSON back.
What this tool does
If you have a PHP class full of typed properties and you need a JSON payload for a test, an API call, or a fixture, hand-writing the array is tedious. Paste the PHP here and you get back valid JSON with every property preserved. Works with a single class, a file of Doctrine entities, a Laravel Eloquent model, or a plain associative array — same result, valid JSON out.
The converter matches what json_encode() does at runtime. Typed properties (PHP 7.4+) and readonly properties (PHP 8.1+) come out as ordinary JSON keys. Integers, floats, booleans, and strings map one-to-one. null becomes JSON null. Nested object instances are expanded into nested JSON objects. Associative arrays become JSON objects; numerically indexed arrays become JSON arrays — the same split json_encode makes.
Modern PHP features are handled too. Backed enums serialize to their backing value (string or int). DateTimeImmutable comes out as an ISO-8601 string. If you paste multiple classes at once — something you might pull off Packagist — each top-level class becomes its own key in the JSON, with nested types expanded. Methods, constants, and static properties are skipped; only instance data makes it into the output.
How to use it
Three steps. Works the same whether you paste ten lines or a full Model file.
Paste your PHP (or try the sample)
Drop your PHP into the left editor as-is. A class definition, new instance, associative array, or multiple classes — all fine. Click Load Sample if you want to see a realistic example first.
Keep the full PHP syntax — namespaces, use statements, visibility modifiers, attributes. The parser ignores the boilerplate it does not need.
Hit Convert
Click the green Convert button. The tool reads the PHP, preserves every property and class, and builds the JSON in one pass. You will see a short loading indicator while it runs.
Copy the JSON
The right panel fills with indented JSON. Copy it straight into a HTTP request body, a PHPUnit fixture, a config file, or your API docs.
When this actually comes in handy
Building API request payloads
You have a CreateOrderRequest DTO and need the matching JSON body for Postman, curl, or an integration test. Paste the class, get the payload.
Seeding Laravel or Symfony
Turn a bunch of <code>new Product(...)</code> factory lines into a JSON seed file for database:seed or fixtures loaders — no more hand-crafted arrays.
Keeping OpenAPI examples honest
Generate JSON examples for your OpenAPI spec directly from your actual DTOs, so the schema examples stay in sync with your code.
Debugging json_encode output
When an object is not serializing the way you expect, paste it here to see the clean shape — then compare against what <code>json_encode()</code> is actually producing.
Common questions
Does it handle PHP 8 typed and readonly properties?
Yes. Typed properties, nullable types (?string), readonly modifiers, and promoted constructor properties are all picked up. The type hint does not change the JSON value — it just tells the parser what to expect.
What about enums?
Backed enums serialize to their backing value (string or int). Pure enums without a backing type are emitted as the case name. This matches the standard json_encode behaviour on a BackedEnum.
Associative array vs indexed array — which becomes an object?
Same rule as json_encode: a 0-indexed sequential array becomes a JSON array; any array with string keys (or non-sequential integer keys) becomes a JSON object.
How are DateTime and DateTimeImmutable serialized?
As ISO-8601 strings, the same format you get from $dt->format(DateTime::ATOM). If your date happens to include a timezone, the offset is preserved in the output.
Can I paste a whole models file?
Yes. Every top-level class becomes its own JSON entry, nested objects expand naturally, and properties set in the constructor are included. Private and protected properties are emitted too — the parser is looking at shape, not visibility.
Is my code stored?
Your PHP is sent to the backend for conversion and is not persisted — we do not log the payload. For sensitive code, give it a once-over before pasting.
Other tools you may need
PHP to JSON is one piece. These pair well with it: