JSON Encode

Free online array to JSON converter. Just load your array and it will automatically get converted to JSON. Load array, get JSON. This tool extracts all JSON values from an array. It ignores all array notation characters and leaves just JSON Super simple!

PHP JSON Encode

The json_encode() function is an inbuilt function in PHP which is used to convert PHP array into JSON format.

JSON stands for JavaScript Object Notation. The JSON data is saved in .json file. The JSON data consist of a series of key & value pairs. The JSON data used to transfer data between the server and the browser.

Here is a example encode PHP array into JSON representation.

Syntax

The syntax for the JSON Encode is following.

json_encode( $value, $option, $depth = 512 )

Parameters

  • value: Required. It specifies the value to be encoded.
  • option: Optional. It specifies a bitmask(JSON_FORCE_OBJECT, JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_NUMERIC_CHECK, JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_PRESERVE_ZERO_FRACTION, JSON_PRETTY_PRINT, JSON_UNESCAPED_LINE_TERMINATORS, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, JSON_THROW_ON_ERROR.)
  • depth: Optional. It specifies the maximum depth. Its value must be greater than zero.

Example

The json_encode() function return a string in json format.

Let's see the following example for PHP array

$arr = array( 'a' => 1 , 'b' => 2 , 'c' => 3 , 'd' => 4 );

echo json_encode($arr)."\n";

The above example will give output