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:
| PHP value | Example | JSON |
|---|---|---|
| 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 / float | 7, 10.0, 0.5 | 7, 10, 0.5 |
| true / false / null | true | true |
How to encode JSON online
- Paste your PHP array as you’d write it in code, with
[ ]orarray( ). A leading$data =, a trailing semicolon and comments are fine. You can paste JSON too. - Turn on flags such as
JSON_PRETTY_PRINT. The line above the result shows the exact call to copy into your code. - Copy the JSON. If PHP would fail, you see
falseand the messagejson_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:
Step 1: With no flags
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
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:
| Flag | What it changes | Without → with |
|---|---|---|
| JSON_PRETTY_PRINT | Line breaks and 4-space indents | one line → indented |
| JSON_UNESCAPED_SLASHES | Leaves / alone | https:\/\/ → https:// |
| JSON_UNESCAPED_UNICODE | Writes é, ü, 日本 as they are | \u00e9 → é |
| JSON_PRESERVE_ZERO_FRACTION | Keeps .0 on whole floats | 10 → 10.0 |
| JSON_FORCE_OBJECT | Writes every array as an object | ["a"] → {"0":"a"} |
| JSON_NUMERIC_CHECK | Turns numeric strings into numbers | "42" → 42 |
| JSON_HEX_TAG / _AMP / _APOS / _QUOT | Escapes < > & ' " for HTML | < → \u003C |
| JSON_THROW_ON_ERROR | Throws JsonException instead of returning false | false → 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()orarray_filter()there are gaps, so PHP writes{"0":"red","2":"blue"}. Usearray_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 passJSON_INVALID_UTF8_SUBSTITUTE.INFandNANalso fail: JSON has no value for them. - Object properties are missing. Only public properties are encoded. Implement
JsonSerializableto 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
- PHP manual: json_encode: arguments, return value and examples
- PHP manual: JSON constants: every flag and what it does
- PHP manual: JsonSerializable: choosing what an object encodes to
- MathWorks: jsonencode: encoding JSON in MATLAB
- RFC 8259: The JSON Data Interchange Format: what JSON allows
Related Tools
Build and preview Valorant crosshair settings.
Search common Vim commands.
Turn JSON rows into bar, line, or pie charts.
Convert WebP files and Base64 data in both directions.
Convert GIF files and Base64 data in both directions.
Save any GIF frame, or all frames, as JPG.