JSON to PHP Array Converter
Turn JSON into a PHP array you can paste into a config file, seeder or test. Runs locally in your browser.
Turn JSON into a PHP array you can paste into a config file, seeder or test. Runs locally in your browser.
This JSON to PHP array converter takes any JSON document and writes it out as PHP source code: a file that starts with <?php and returns the data as an array literal. It uses the short [ ] syntax (PHP 5.4+), four-space indentation and a trailing comma after every element, the style PSR-12 and Laravel config files use, so the output drops into a codebase without reformatting.
.php file, or paste just the array part after return into existing code.[{"id":1,"name":"Ada","tags":["x"]},{"id":2,"name":"Bo","ok":true}]
becomes
<?php
return [
[
'id' => 1,
'name' => 'Ada',
'tags' => [
'x',
],
],
[
'id' => 2,
'name' => 'Bo',
'ok' => true,
],
];
Objects become associative arrays with quoted keys, JSON arrays become lists without explicit indexes, and true, false and null map to the PHP constants.
{"app_name":"Shop","debug":false,"ratio":1.0,"price":19.5,
"path":"C:\\temp\\it's","empty":{},"0":"zero",
"db":{"host":"localhost","port":5432}}
becomes
<?php
return [
0 => 'zero',
'app_name' => 'Shop',
'debug' => false,
'ratio' => 1,
'price' => 19.5,
'path' => 'C:\\temp\\it\'s',
'empty' => [],
'db' => [
'host' => 'localhost',
'port' => 5432,
],
];
Four details are worth noticing, because they are where JSON and PHP arrays disagree:
"0" is written as the integer 0, which is what PHP itself does with numeric string keys, and it moves to the top because the browser's JSON parser orders integer keys first. If key order matters, avoid numeric keys or reorder by hand.\ and ' are escaped. A $ in a value is never interpolated, and line breaks inside a string are kept as real line breaks, which PHP accepts inside quotes.1.0 becomes 1. JavaScript does not keep the difference between 1.0 and 1, so a whole-number float is written as an int. Add .0 back if your code checks is_float().{} becomes [], and json_encode([]) later produces [], not {}. Use new \stdClass() or (object) [] where an empty JSON object must survive a round trip.Save the output as config/shop.php in a Laravel app and read values with dot notation:
$host = config('shop.db.host'); // 'localhost'
// plain PHP
$settings = require __DIR__ . '/shop.php';
echo $settings['db']['port']; // 5432
// back to JSON
echo json_encode($settings, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
json_decode($json, true) gives the same array at run time. A literal is better when the data belongs in the codebase: config/*.php files, database seeders, test fixtures and PHPUnit data providers, translation files, or anywhere you want the data cached by OPcache and reviewed line by line in diffs. Keep json_decode for data that arrives at run time from an API or a database column.
Compared with var_export(), which prints the old array ( syntax with two-space indentation and numeric indexes on every list element, this output is shorter and matches modern coding standards.
A common use is moving JSON test cases into PHPUnit. Convert an array of cases, paste the array after return in a static provider method, and each element becomes one run of the test:
public static function priceCases(): array
{
return [
['qty' => 1, 'unit' => 9.5, 'expected' => 9.5],
['qty' => 3, 'unit' => 2, 'expected' => 6],
];
}
The converter puts every element on its own lines; collapse short rows like these by hand if you prefer one case per line.
Integers larger than 9,007,199,254,740,991 cannot be represented exactly in the browser, so they are written in float notation such as 1.2345678901234567E19 and lose their last digits. Quote big IDs as strings in the JSON before converting; see JSON big integer precision for why. Unicode text is written as-is, so save the file as UTF-8.
To generate typed classes from the same JSON for C#, Java, TypeScript, Go or Python, use the class generator. To tidy an existing PHP file, use the PHP beautifier. If the JSON will not parse, check it first in the JSON validator, and if the target is a YAML config rather than PHP, use JSON to YAML.
Save it, for example as data.php, and write $data = require 'data.php';. In Laravel, a file in config/ is available as config('filename.key').
Use json_decode($json) without the second argument to get stdClass objects at run time. This converter always produces associative arrays.
PHP stores numeric string keys as integers anyway, so the converter writes them as integers. The browser's JSON parser lists integer-like keys before other keys, which is why it appears first.
The short array syntax needs PHP 5.4 or later, and trailing commas inside arrays have always been allowed. For PHP 5.3 and older, replace each [ with array( and each ] with ).
No. The conversion runs in your browser and nothing is sent to a server.