JSON to PHP Class

Turn JSON into a PHP array or typed PHP 8 classes

About JSON to PHP Class

JSON to PHP Class turns JSON into PHP code in two ways. PHP array, selected by default, writes your data as a $data = [ ... ]; literal in short array syntax, matching what json_decode($json, true) returns: strings escaped, numbers as written (1.10 stays 1.10), integers above PHP_INT_MAX kept as strings. PHP classes writes final PHP 8 classes, one per nested object, with typed promoted properties, nullable types for keys that are missing or sometimes null, and a fromArray() factory that reads the original keys. This page does not use quicktype: the converter is the site's own code, run in your browser.

Example

PHP array output; the ID is larger than PHP_INT_MAX, so it is kept as a string.

JSON input

{"name": "Ada", "roles": ["admin", "dev"], "price": 1.10, "id": 12345678901234567890}

PHP output

<?php

// Integers beyond PHP_INT_MAX are written as strings, as json_decode() does with JSON_BIGINT_AS_STRING; as a float they would lose digits.
$data = [
    'name' => 'Ada',
    'roles' => [
        'admin',
        'dev',
    ],
    'price' => 1.10,
    'id' => '12345678901234567890',
];

How to convert JSON to a PHP array or class

  1. 1Paste the JSON object or array you want as PHP into "JSON input".
  2. 2Keep "PHP array" to get your values, or pick "PHP classes" to get typed classes.
  3. 3Click "Convert" to fill "PHP output".
  4. 4Click "Copy", or "Download" to save it as "data.php".

Formats and limits

Output modes
PHP array (default) or PHP classes
Input
JSON text; class mode needs an object or an array of objects
PHP version
Array: 5.4 or later. Classes: 8.0 or later (constructor promotion, named arguments, mixed)
Large integers
PHP array: values above PHP_INT_MAX are written as strings, with a comment. PHP classes: typed string, with a comment, and the Usage line decodes with JSON_BIGINT_AS_STRING so the digits are kept

Where your JSON is processed

This converter is plain JavaScript executed by your browser, with no library to download, and the JSON never leaves your computer. If a message such as Invalid JSON or the class-mode warning is displayed, that message, stripped of quoted text, long numbers, and addresses, is reported to our error log, along with the tool's name and your browser's name and version.

Frequently asked questions

Can I get a PHP array with my actual data?

Yes. Keep "PHP array" selected: the output is $data = [ 'key' => value, ... ]; with nested arrays for nested objects. For a config file that must return an array, replace $data = with return.

Which PHP version do the classes need?

8.0 or later. They use constructor property promotion, named arguments in fromArray(), and the mixed type, all introduced in PHP 8.0. The array output only needs short array syntax, available since PHP 5.4.

What happens to very large integers?

In the PHP array output they become strings: a JSON integer above PHP_INT_MAX would lose digits as a PHP float, so it is written in quotes, as json_decode does with JSON_BIGINT_AS_STRING, and a comment says so. In class mode the property is typed string, a comment says so, and the Usage line calls json_decode with JSON_BIGINT_AS_STRING to keep the digits.

Can class mode convert a list of numbers?

No. Classes need an object or an array of objects. For a list of numbers or strings, or a single value, the page shows a message asking you to use the array output instead.

Tips and tricks

  • ✓In class mode, paste several array elements: keys present in only some of them become nullable properties.
  • ✓Keys such as first-name become camelCase properties (firstName), while fromArray() still reads the original key.