Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

Free tool · Developer tools

PHP array to JSON converter

Paste into this PHP array to JSON converter and read the result at once. The starting example has 11 keys over 3 arrays, and the box below it shows the JSON that json_encode writes for it, with the arrows replaced by colons and the mixed keys sorted into an array or an object.

  • Free
  • No sign-up
  • Runs in your browser

By Maaz Zindani · Updated

Short [ ] or array( ) syntax, var_export output, a return or $var = line. Variables, function calls and constants are refused because nothing is run.
json_encode flags
JSON
  • 11 keys
  • 15 lines
  • 223 bytes
  • Depth 3
  • PHP wrapper removed
{
    "site": "demo",
    "debug": false,
    "port": 8080,
    "ratio": 0.75,
    "paths": [
        "/var/www",
        "/tmp"
    ],
    "3": "three",
    "db": {
        "host": "localhost",
        "opts": null
    }
}

What changed from PHP to JSON
PHPJSON
=> pairs (9)Colons
array( ) form (1)Square brackets
Arrays with gaps or text keys (2)JSON objects
Keys left out (2)Counted from 0
Trailing commas (1)Removed

How to use the PHP Array to JSON Converter

  1. Paste the array into PHP array. A leading <?php, a return or a $config = prefix and the closing semicolon are fine.
  2. Choose a Layout. The first option is the 4 space pretty print that PHP itself writes, the last one is the compact form that json_encode writes with no flags.
  3. Under json_encode flags decide whether / and non-ASCII characters are escaped, and whether every array should become an object.
  4. Check the table of changes under the result, then use the Copy JSON button or save the result as a .json file.

Turning a PHP array into JSON without running any PHP

In a PHP script the answer is one call, json_encode($array). That only works inside a live PHP process, with the array already in a variable. Often you only have text: a config file from a repository, the output of var_export, or an array copied out of a bug report. This page reads that text itself and writes what json_encode would have written, without evaluating anything.

The example in the box is a config array. Its compact JSON is 132 characters long: {"site":"demo","debug":false,"port":8080,"ratio":0.75,"paths":["/var/www","/tmp"],"3":"three","db":{"host":"localhost","opts":null}}. The key 3 sits between text keys, so the top level cannot be a JSON list and becomes an object, while the inner paths array counts 0 and 1 and stays a list. The array('host' => ...) form and the short form are read the same way, and both end up as ordinary JSON brackets.

When does a PHP array become a JSON list and when an object?

PHP has one array type that serves as list and as dictionary. json_encode looks at the keys. If they are the integers 0, 1, 2 and so on in that order, the output is a JSON array. Any other shape, such as a text key, a gap, a different order or a start at 1, gives an object whose keys are written as strings. An empty array is written as [], and only an (object) cast or the Force objects flag turns it into {}.

Keys are also changed before they are stored. A numeric string such as "8" becomes the integer 8, but "08" and "+4" stay text. A float key loses its fraction, true and false become 1 and 0, and null becomes an empty string. A key that appears twice keeps its first position and takes the last value. When a key is left out, PHP uses the highest integer key so far plus one. After a negative first key such as -5, PHP 8.3 and later continue at -4, while older versions continue at 0.

PHP input and the compact JSON written for it
PHP arrayJSON
['8' => 'a', '08' => 'b']{"8":"a","08":"b"}
[true => 'x', false => 'y']{"1":"x","0":"y"}
[null => 'n']{"":"n"}
[2.9 => 'f']{"2":"f"}
[-5 => 'a', 'b']{"-5":"a","-4":"b"}
[3 => 'x', 'y', 9 => 'z', 'w']{"3":"x","4":"y","9":"z","10":"w"}
[1 => 'a', 0 => 'b']{"1":"a","0":"b"}
[0 => 'a', 1 => 'b']["a","b"]

Why does json_encode write \/ and \u00e9 in my output?

Without flags, json_encode escapes the forward slash as \/ and writes every character above U+007F as a \u code, using a surrogate pair for emoji. Both spellings decode to the same text, so the choice only affects how readable the file is. The two flag boxes reproduce JSON_UNESCAPED_SLASHES and JSON_UNESCAPED_UNICODE in reverse: tick them to get the escaped spelling, leave them empty to get plain characters. The line separators U+2028 and U+2029 stay escaped even when the rest is plain, which is what PHP does.

The same string with the flag boxes empty and ticked
CasePHPPlainEscaped
Slash in a path['/var/www']["/var/www"]["\/var\/www"]
Accented letter['café']["café"]["caf\u00e9"]
Emoji['😀']["😀"]["\ud83d\ude00"]
Line separator U+2028["\u{2028}"]["\u2028"]["\u2028"]

PHP array syntax this converter accepts and rejects

It reads array() and [ ] in any nesting, single and double quoted strings with their escapes (\n, \t, \e, \xE9, octal codes and \u{1F600}), integers in decimal, hex, octal and binary with digit separators, floats, true, false and null in any letter case, (object) casts, stdClass::__set_state output, comments and trailing commas. An integer above 9223372036854775807 turns into a float, as in PHP.

Anything that depends on a live PHP process is refused with the line and column: variables, function calls, class constants such as PHP_INT_MAX, heredoc text, spread, and a double quoted string that contains $name, because PHP would insert a value there. A string whose bytes are not valid UTF-8 is refused too, since json_encode returns false for it.

The rest of your JSON work stays in the browser as well. The JSON formatter checks or minifies the result, the JSON to Python dict converter goes to another language, and JSON to CSV turns a list of objects into a table.

How floats are written
PHP floatJSON number
1.01
1e1610000000000000000
1e171.0e+17
0.00010.0001
1e-51.0e-5
0.1 + 0.20.30000000000000004
-0.0-0

Use it with

Questions

How do I convert a PHP array to JSON?

In PHP call json_encode($array). To convert text you already have, paste the array into this page and copy the JSON. It follows the rules json_encode uses for lists, objects, keys and numbers.

Why is my PHP array an object in JSON?

json_encode only writes a JSON array when the keys are 0, 1, 2 and so on in order. A text key, a gap, a different order or a start at 1 gives an object with string keys.

Can I paste the output of var_export?

Yes. The array ( ... ) layout with numbered or quoted keys, nested arrays and stdClass::__set_state output are all read. print_r and var_dump output is a different format and is not read.

Does this run my PHP code?

No. A small parser reads the literal, so nothing in the text is executed. Variables, function calls and constants are refused because they need PHP to find their value.

What happens to null, true and false keys?

PHP stores a true key as 1, a false key as 0 and a null key as an empty string. The JSON shows those stored keys, and a key that repeats keeps only its last value.

Why are the slashes and accents escaped in PHP output?

json_encode escapes / and non-ASCII characters unless it is given JSON_UNESCAPED_SLASHES and JSON_UNESCAPED_UNICODE. The flag boxes switch each spelling on or off; both decode to the same text.

The tool's results are computed in your browser by the code on this page, using the sources listed above; the explanations are checked against the tool's own output.