Skip to content

JSON Encode Online

Paste a PHP array and see exactly what json_encode() returns, with any combination of flags. Find out why slashes get backslashes, why é turns into é and why an array becomes an object.

echo json_encode($data, JSON_PRETTY_PRINT);

PHP value · array with 7 items

More flags

json_encode($data)

{"id":7,"name":"Zo\u00eb Brown","website":"https:\/\/example.com\/zoe","tags":["php","json"],"price":10,"active":true,"manager":null}

Output matches PHP 8’s json_encode() byte for byte. Everything runs in your browser; nothing is uploaded.

What does json_encode() do?

json_encode() turns a PHP value, usually an array, into a JSON string you can send from an API, save to a file or pass to JavaScript. It is the opposite of json_decode(). This is how each PHP type is written:

How json_encode() writes each PHP value as JSON
PHP valueExampleJSON
List (keys 0, 1, 2…)['php', 'json']["php","json"]
Array with other keys['id' => 7]{"id":7}
Empty array[][]
stdClass object(object) []{}
String'a/b é'"a\/b \u00e9"
Integer / float7, 10.0, 0.57, 10, 0.5
true / false / nulltruetrue

How to encode JSON online

  1. Paste your PHP array as you’d write it in code, with [ ] or array( ). A leading $data =, a trailing semicolon and comments are fine. You can paste JSON too.
  2. Turn on flags such as JSON_PRETTY_PRINT. The line above the result shows the exact call to copy into your code.
  3. Copy the JSON. If PHP would fail, you see false and the message json_last_error_msg() would return.

Variables and function calls can’t run here, so paste their values instead.

Example: clean up json_encode() output

You encode this profile (the example in the tool) for an API response:

$data = [ 'id' => 7, 'name' => 'Zoë Brown', 'website' => 'https://example.com/zoe', 'tags' => ['php', 'json'], 'price' => 10.0, 'active' => true, 'manager' => null, ];

Step 1: With no flags

{"id":7,"name":"Zo\u00eb Brown","website":"https:\/\/example.com\/zoe","tags":["php","json"],"price":10,"active":true,"manager":null}

It is valid JSON, but hard to read: the ë in Zoë became ë, every / became \/, and the price 10.0 lost its decimal.

Step 2: Add flags

json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION)
{ "id": 7, "name": "Zoë Brown", "website": "https://example.com/zoe", "tags": [ "php", "json" ], "price": 10.0, "active": true, "manager": null }

Same data, readable output. Both versions decode to exactly the same values, so either is safe to send.

json_encode() flags

Combine flags with |, for example JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES. The ones people use most:

Common json_encode() flags and what they change
FlagWhat it changesWithout → with
JSON_PRETTY_PRINTLine breaks and 4-space indentsone line → indented
JSON_UNESCAPED_SLASHESLeaves / alonehttps:\/\/ → https://
JSON_UNESCAPED_UNICODEWrites é, ü, 日本 as they are\u00e9 → é
JSON_PRESERVE_ZERO_FRACTIONKeeps .0 on whole floats10 → 10.0
JSON_FORCE_OBJECTWrites every array as an object["a"] → {"0":"a"}
JSON_NUMERIC_CHECKTurns numeric strings into numbers"42" → 42
JSON_HEX_TAG / _AMP / _APOS / _QUOTEscapes < > & ' " for HTML< → \u003C
JSON_THROW_ON_ERRORThrows JsonException instead of returning falsefalse → exception

Be careful with JSON_NUMERIC_CHECK: it also changes phone numbers and ZIP codes, so "01234" becomes 1234. Cast the fields you mean with (int) instead.

Common json_encode() problems

  • An array turns into an object. JSON lists can only have keys 0, 1, 2… in order. After unset() or array_filter() there are gaps, so PHP writes {"0":"red","2":"blue"}. Use array_values($colors) to renumber it. Load Array with gaps above to see it.
  • It returns false. Usually a string isn’t valid UTF-8 (text from an old database or a Windows-1252 file), giving Malformed UTF-8 characters. Convert it with mb_convert_encoding($s, 'UTF-8', 'Windows-1252'), or pass JSON_INVALID_UTF8_SUBSTITUTE. INF and NAN also fail: JSON has no value for them.
  • Object properties are missing. Only public properties are encoded. Implement JsonSerializable to choose what goes out.
  • The output has \" everywhere. You encoded a string that was already JSON. Encode the array once, not its JSON.

To return JSON from a PHP script or API:

header('Content-Type: application/json; charset=utf-8');
echo json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
exit;

Encoding JSON in MATLAB and other languages

MATLAB’s jsonencode works like PHP’s: a struct becomes a JSON object and an array becomes a JSON array. Other languages have their own function:

% MATLAB (PrettyPrint needs R2021a or later)
s.name = "Zoë"; s.tags = ["php", "json"];
txt = jsonencode(s)                        % {"name":"Zoë","tags":["php","json"]}
txt = jsonencode(s, PrettyPrint=true)

# Python
json.dumps(data, ensure_ascii=False, indent=4)

// JavaScript
JSON.stringify(data, null, 4)

// C#
JsonSerializer.Serialize(data)

For a closer look at indentation, naming and null settings in C#, Java and Python, see JSON Serialize Online.

Frequently asked questions

Why does json_encode add backslashes?

By default PHP escapes every / as \/, which is still valid JSON and decodes back to /. Add JSON_UNESCAPED_SLASHES to stop it. If you see \" everywhere instead, the data was encoded twice.

How do I pretty print JSON in PHP?

Pass JSON_PRETTY_PRINT: echo json_encode($data, JSON_PRETTY_PRINT); PHP always indents with 4 spaces. Wrap it in <pre> if you are printing it into an HTML page.

What is the difference between json_encode() and serialize()?

json_encode() writes standard JSON that any language can read. serialize() writes PHP’s own format (a:1:{s:2:"id";i:7;}), which keeps PHP classes but only PHP can read. Never unserialize() data from users.

How do I keep é and other accents in json_encode output?

Add JSON_UNESCAPED_UNICODE. Without it, PHP writes every non-ASCII character as a \u code such as \u00e9. Both forms decode to the same text.

Sources