9951 explained code solutions for 126 technologies


php-mysqlHow to escape JSON in PHP and MySQL?


JSON (JavaScript Object Notation) is a lightweight data-interchange format used for exchanging data between a server and a client. In PHP and MySQL, escaping JSON is done by using the json_encode() function. This function takes a PHP value and returns a JSON string representation of it.

$data = array("name" => "John", "age" => 30);
$json_data = json_encode($data);
echo $json_data;

Output example

{"name":"John","age":30}

The json_encode() function takes the following parameters:

  • $value: The value to be encoded.
  • $options: Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR.

The json_encode() function returns a JSON encoded string on success and FALSE on failure.

Helpful links

Edit this code on GitHub