JSON to PHP Converter for Arrays and Typed Classes

Backend code that consumes an API often starts with a JSON sample and a hand-typed PHP array. Keys get missed, strings need escaping, and the choice between int and float is guessed from the first payload. This converter parses the JSON in your browser and emits a PHP associative array, a typed class, or a ready-to-paste json_decode snippet. Nothing is uploaded, so internal IDs and customer names stay on your machine.

Convert JSON to PHP in the Browser

How It Works

The input is parsed with the browser's own JSON parser, so the accepted syntax matches what any modern JavaScript runtime accepts. In array mode the parsed value is walked recursively and printed as PHP literal syntax: strings become double-quoted literals, true and false become the PHP keywords, null becomes null, and numbers print without quotes.

Double-quoted PHP strings expand variables that start with a dollar sign, so the emitter escapes the backslash, the double quote, the dollar sign, and control characters such as newline, carriage return, and tab. Other low control characters print as brace-notation unicode escapes. Whole numbers stay ints and decimal numbers keep their fractional part as floats.

PHP has a single array type that covers both lists and maps, so an empty JSON object prints as an empty array literal in array mode. Class mode turns each top-level key into a public property, replaces characters that PHP identifiers reject with the _ character, and prefixes the name with a dollar sign. Typed properties such as int, float, string, bool, and array require PHP 8.0 or newer.

Decode mode wraps the trimmed input in one json_decode call with a depth of 512 and the JSON_THROW_ON_ERROR flag, so malformed data raises an exception instead of returning null. The whole JSON text is escaped into a single double-quoted string, which keeps the snippet self-contained.

Before and After

Before (JSON from an API)
{ "total": 49.99, "paid": true,
  "buyer": { "id": 7 } }
After (array mode)
[
  "total" => 49.99,
  "paid" => true,
  "buyer" => [
    "id" => 7,
  ],
];
Before (same JSON, class name typed)
class name: Invoice
mode: PHP class
keys: total, paid, buyer
After (class mode output)
class Invoice {
  public float $total = 49.99;
  public bool $paid = true;
  public array $buyer = [
    "id" => 7,
  ];
}

Use Cases

Developer Production Workflow
Seed a fixture array for a PHPUnit test Convert a vendor webhook payload into a typed class Paste a decode snippet into a cron script
Map a REST response without hand-escaping strings Regenerate models after a vendor schema change Pair with the JSON validator before converting
Compare array mode and class mode on the same sample Standardize indent width across a PHP codebase Convert the same JSON to Python and C# for the rest of the stack

Common Mistakes

  • Expecting empty objects to stay objects. PHP prints both an empty JSON object and an empty JSON array as an empty array literal. If you must tell them apart at runtime, use the decode snippet with the second argument set to false.
  • Running class mode on a non-object root. Class mode needs a JSON object at the top level. A root array or a bare number produces no output, and the issues box explains what the mode expects.
  • Deleting the dollar sign from property names. PHP variables require it, so the emitter prefixes every property automatically. A pasted class without the dollar signs fails to parse.
  • Ignoring typed properties on older PHP. A declaration such as a public int property fails before PHP 8.0. Remove the type words or move the project to a supported release before pasting.
  • Hand-editing string escapes after copying. The escaped dollar sign protects against variable interpolation inside double-quoted strings. Removing it changes the string whenever the value contains a dollar sign.

Best Practices

  • 1.Start in array mode to check the structure, then switch to class mode once the keys and number types look right.
  • 2.Set the class name before copying so the pasted code already matches your naming convention and namespace.
  • 3.Paste decode snippets where a thrown JsonException is acceptable, and catch it at the request boundary instead of deep in a helper.
  • 4.Regenerate after an upstream API change rather than editing the array by hand, so new keys are not missed and string escaping stays consistent.

Frequently Asked Questions

What do the assoc true and false values mean in the decode snippet?

The second argument of json_decode controls what PHP returns for JSON objects. true builds associative arrays, so you read values as $data['role']. false builds stdClass objects, so you read values as $data->role. The snippet passes true, and changing that single word after pasting is all it takes to switch.

What is the difference between a PHP array and a PHP object here?

PHP arrays cover both ordered lists and key-value maps, so a JSON array becomes a sequential array and a JSON object becomes an array with string keys. A PHP object is an instance of a class with properties. Array mode always emits arrays; class mode emits a real class so your code gets properties and dot-free access through the typed fields.

Why does an empty JSON object become an empty PHP array?

PHP has one array literal syntax for both lists and maps, and both an empty JSON object and an empty JSON array print as []. There is no separate empty-object literal. If you need to tell the two apart at runtime, use the decode snippet with the second argument set to false, which turns non-empty objects into stdClass while arrays stay arrays.

Do the typed properties require a specific PHP version?

Yes. Property types such as int, float, string, bool, and array were added in PHP 8.0. On PHP 7 you would delete the type words and keep the same property names and default values. The structure of the generated class does not change between those versions.

How do I paste the output into a Laravel controller?

Copy the array output and assign it to a variable inside the controller method, then pass it to your validation or response helpers. If the request body arrives as a raw JSON string, use the decode snippet first and feed the result to Validator::make or your form request. Generate one class per model if you prefer classes, and keep the names unique inside the namespace.

Why are strings emitted with double quotes?

PHP double-quoted strings interpolate variables that begin with a dollar sign and understand escape sequences such as newline and tab. The emitter escapes backslashes, double quotes, dollar signs, and control characters, so each printed string holds exactly the JSON text. Single-quoted strings would not need the dollar escape, but double quotes are the common style and the escapes stay readable.

How does this handle very large JSON?

Parsing and printing run in the tab, so the time follows the size of your input and the browser parser. Multi-megabyte payloads can pause the interface for a moment while the tree is walked, and the decode snippet copies the entire document into one escaped string, which grows quickly. For huge files, stick with array mode and copy in a single pass.

Related JSON and developer tools