Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. json_encode() / json_decode()

json_encode() / json_decode() Since: PHP 5(2004)

Functions for converting PHP values to JSON-formatted strings and JSON strings back to PHP values. They are frequently used in API development and for exchanging data with JavaScript.

Syntax

// Converts a PHP value to a JSON string.
json_encode($value, $flags, $depth);

// Converts a JSON string to a PHP value.
json_decode($json, $associative, $depth, $flags);

// Returns the error code from the last JSON operation.
json_last_error();

// Returns the error message from the last JSON operation.
json_last_error_msg();

Function List

FunctionDescription
json_encode($value, $flags, $depth)Converts a PHP value to a JSON string. The second argument accepts encoding option flags, and the third argument sets the maximum nesting depth.
json_decode($json, $associative, $depth, $flags)Converts a JSON string to a PHP value. Passing true as the second argument returns an associative array instead of an object.
json_last_error()Returns the error code produced by the last call to json_encode() or json_decode().
json_last_error_msg()Returns the error message from the last JSON operation as a string.

Common Option Constants

ConstantDescription
JSON_UNESCAPED_UNICODEOutputs multibyte characters (such as non-ASCII text) as-is without escaping them.
JSON_PRETTY_PRINTOutputs formatted JSON with indentation, making it easier to read during debugging.
JSON_UNESCAPED_SLASHESOutputs forward slashes / as-is without escaping them.
JSON_THROW_ON_ERRORThrows a JsonException on error instead of silently failing. Available in PHP 7.3 and later.

Sample Code

<?php
// Converts an associative array to a JSON string.
$data = ['name' => 'Taro Yamada', 'age' => 30, 'city' => 'Tokyo'];
$json = json_encode($data, JSON_UNESCAPED_UNICODE);
echo $json; // Outputs: {"name":"Taro Yamada","age":30,"city":"Tokyo"}

// Outputs the JSON formatted with indentation, without escaping non-ASCII characters.
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

// Converts a JSON string to an object.
$jsonStr = '{"name":"Hanako Sato","age":25}';
$obj = json_decode($jsonStr);
echo $obj->name; // Outputs: Hanako Sato

// Passing true as the second argument returns an associative array.
$arr = json_decode($jsonStr, true);
echo $arr['age']; // Outputs: 25

// Detects an error from an invalid JSON string.
$invalid = "{'name': 'test'}"; // Single quotes are not valid JSON.
$result = json_decode($invalid);
if (json_last_error() !== JSON_ERROR_NONE) {
	echo json_last_error_msg(); // Outputs: Syntax error
}

// Using JSON_THROW_ON_ERROR lets you handle errors with exceptions.
try {
	$decoded = json_decode($invalid, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
	echo $e->getMessage(); // Outputs: Syntax error
}

Notes

json_encode() converts PHP arrays and objects to a JSON string, while json_decode() converts a JSON string back to a PHP value. When working with non-ASCII text, always pass JSON_UNESCAPED_UNICODE. Without it, characters will be escaped as \uXXXX sequences.

When the second argument of json_decode() is omitted, the result is returned as an object. Passing true returns an associative array instead. Since working with arrays is more common, passing true as the second argument is the recommended approach.

For error handling, you can check errors using json_last_error(), or — in PHP 7.3 and later — use JSON_THROW_ON_ERROR to throw an exception. The exception-based approach leads to cleaner code and makes errors harder to miss. See try / catch for the basics of exception handling.

If you find any errors or copyright issues, please .