Skip to content
OnlineToolStudio

PHP json_encode

runs in your browserno sign-upfree

Encode a PHP array, print_r() or var_dump() output to JSON exactly like json_encode() — with JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, UNICODE and other flags.

PHPecho json_encode($data, JSON_PRETTY_PRINT);

Read as PHP array · encoded to JSON

PHP array, print_r, var_dump or JSON249 chars
JSON

About this tool

This tool shows exactly what PHP’s json_encode() returns. Paste a PHP array (array() or [ ] syntax, or var_export output), the output of print_r() or var_dump() — even with stdClass objects — or JSON, and tick the same flags you use in code: JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, JSON_PRESERVE_ZERO_FRACTION, JSON_FORCE_OBJECT, JSON_NUMERIC_CHECK and the JSON_HEX_* flags. The matching json_encode() call is shown above the result, ready to copy.

How to use

  1. Paste a PHP array, print_r() or var_dump() output, or JSON — the tool detects which.
  2. Tick the json_encode flags you need.
  3. Copy the JSON, or copy the json_encode() call for your code.

FAQ

Why does json_encode write \/ and \u00e9?

By default PHP escapes every / as \/ and every non-ASCII character as \uXXXX. Tick "Unescaped slashes" (JSON_UNESCAPED_SLASHES) and "Unescaped Unicode" (JSON_UNESCAPED_UNICODE) to keep them as they are — the output stays valid JSON either way.

Why is my array encoded as an object { } instead of a list [ ]?

json_encode only writes a list when the keys are exactly 0, 1, 2… in order. Any other keys — strings, gaps, a different order — make an object. JSON_FORCE_OBJECT makes every array an object, even lists and empty arrays.

Can I convert print_r() or var_dump() output to JSON?

Yes. var_dump output is read exactly, with every type. print_r output doesn’t show types, so whole numbers become integers and everything else a string — true prints as 1, and false/null print as nothing. Use var_dump output when exact types matter.

Where are my private and protected properties?

json_encode() only includes an object’s public properties, so private and protected ones are left out — the status line says how many. Implement JsonSerializable in your class to control this.

Does it match PHP exactly?

Yes — escaping, float formatting (1.0e+25, 0.30000000000000004), pretty-print spacing, empty arrays and every flag were checked against real PHP 8 output. INF and NAN give an error, as json_encode() returns false for them.

Related tools

All tools