JSON to PHP Array Converter — Free Online

JSON to PHP Array Converter

Paste a JSON object and get back a PHP file that returns the same data as a return [...]; array. Objects become 'key' => value pairs, arrays become lists, and null, true and false stay null, true and false. The conversion runs in your browser, and the input is not sent anywhere.

How the output is built

  • The output starts with <?php, a blank line, and return followed by the array and a semicolon. Saved as a .php file, it returns the array when included.
  • Objects and arrays are indented two spaces per level, one item per line. Items are separated by commas, and the last item has no trailing comma.
  • Keys keep the input order, except keys that look like whole numbers. Those come first, in ascending order, because that is how JavaScript orders object keys.
  • An empty object and an empty array both become []. PHP has one array type, so the difference is lost.
  • String values are single-quoted. Backslashes and single quotes are escaped. A value with a newline, carriage return or tab is double-quoted instead, with \n, \t and the other control characters escaped, and a $ is escaped so PHP does not expand it.
  • Keys are escaped the same way as values, so an apostrophe in a key becomes \'.
  • Numbers are printed with JavaScript's number formatting. Gotcha 1 covers what that changes.
  • If the input is not valid JSON, the output is an Invalid JSON message with the parser's error, not PHP.

Worked example

Input:

{
  "app": {
    "name": "Shop",
    "debug": false,
    "timezone": "UTC"
  },
  "database": {
    "host": "127.0.0.1",
    "port": 3306,
    "replicas": ["10.0.0.2", "10.0.0.3"]
  }
}

Output, exactly as the converter prints it:

<?php
 
return [
  'app' => [
    'name' => 'Shop',
    'debug' => false,
    'timezone' => 'UTC'
  ],
  'database' => [
    'host' => '127.0.0.1',
    'port' => 3306,
    'replicas' => [
      '10.0.0.2',
      '10.0.0.3'
    ]
  ]
];

Gotchas

1. Whole-number floats lose their decimal point

1.0 is written as 1, so a float in the JSON becomes an integer in PHP.

{ "ratio": 1.0 }
'ratio' => 1

Large integers lose digits too. 12345678901234567890 is written as 12345678901234567000, because JavaScript stores numbers as 64-bit floats. Keep such values as strings in the JSON.

2. A newline in a value is double-quoted

{ "msg": "line1\nline2" }

The value contains a real newline after JSON parsing, so the converter switches it to a PHP double-quoted string, where \n is a line break:

'msg' => "line1\nline2"

Single quotes and dollar signs are safe in that output. The apostrophe in a key is escaped as well, so { "it's": "x" } becomes 'it\'s' => 'x'.

4. Empty objects become empty arrays

{ "opts": {} }
'opts' => []

PHP code cannot tell the two apart, so a round trip through PHP changes the JSON type.

FAQ

Is my JSON uploaded? No. The converter parses the JSON and builds the PHP text in the page with plain JavaScript.

Does the output check out as PHP? The converter does not run PHP, so run php -l on the saved file before you deploy it. The escaping covers apostrophes, backslashes and control characters, so the syntax should hold for any JSON string.

How do I convert PHP back to JSON? Use PHP Array to JSON.