{"id":1409,"date":"2021-05-04T01:15:19","date_gmt":"2021-05-04T01:15:19","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=1409"},"modified":"2021-07-21T09:21:04","modified_gmt":"2021-07-21T09:21:04","slug":"php-json","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-oop\/php-json\/","title":{"rendered":"PHP JSON"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to convert data in PHP to JSON data and vice versa using the PHP JSON extension.<\/p>\n\n\n\n<p>JSON stands for JavaScript Object Notation. JSON is designed as a lightweight data-interchange format.<\/p>\n\n\n\n<p>JSON is built on two structures:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A collection of name\/value pairs called JSON objects. JSON objects are equivalent to <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-associative-arrays\/\">associative arrays<\/a> in PHP.<\/li><li>An ordered list of values called arrays. They&#8217;re equivalent to <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-array\/\">indexed arrays<\/a> in PHP.<\/li><\/ul>\n\n\n\n<p>The JSON format is human-readable and easy for computers to parse. Even though JSON syntax derives from <a href=\"https:\/\/www.javascripttutorial.net\/\" target=\"_blank\" rel=\"noreferrer noopener\">JavaScript<\/a>, it&#8217;s designed to be language-independent.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-json-extension'>PHP JSON extension <a href=\"#php-json-extension\" class=\"anchor\" id=\"php-json-extension\" title=\"Anchor for PHP JSON extension\">#<\/a><\/h2>\n\n\n\n<p>PHP natively supports JSON via the JSON extension. The JSON extension provides you with some handy functions that convert data from PHP to JSON format and vice versa.<\/p>\n\n\n\n<p>Since the JSON extension comes with PHP installation by default, you don&#8217;t need to do any extra configuration to make it works.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='converting-php-variables-to-json-using-json_encode-function'>Converting PHP variables to JSON using json_encode() function <a href=\"#converting-php-variables-to-json-using-json_encode-function\" class=\"anchor\" id=\"converting-php-variables-to-json-using-json_encode-function\" title=\"Anchor for Converting PHP variables to JSON using json_encode() function\">#<\/a><\/h2>\n\n\n\n<p>To get a JSON representation of a variable, you use the <code>json_encode()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">json_encode ( mixed $value , int $flags = <span class=\"hljs-number\">0<\/span> , int $depth = <span class=\"hljs-number\">512<\/span> ) : string|<span class=\"hljs-keyword\">false<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example uses the <code>json_encode()<\/code> function to convert an indexed array in PHP to JSON format:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$names = &#91;<span class=\"hljs-string\">'Alice'<\/span>, <span class=\"hljs-string\">'Bob'<\/span>, <span class=\"hljs-string\">'John'<\/span>];\n$json_data = json_encode($names);\n\n<span class=\"hljs-comment\">\/\/ return JSON to the browsers<\/span>\nheader(<span class=\"hljs-string\">'Content-type:application\/json'<\/span>);\n<span class=\"hljs-keyword\">echo<\/span> $json_data;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, define an array of strings that consists of three elements.<\/li><li>Second, convert the array to JSON using the <code>json_encode()<\/code> function.<\/li><li>Third, return the JSON data to the browsers by setting the content type of the document to <code>appplication\/json<\/code> using the <code>header()<\/code> function.<\/li><\/ul>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">&#91;\r\n   <span class=\"hljs-string\">\"Alice\"<\/span>,\r\n   <span class=\"hljs-string\">\"Bob\"<\/span>,\r\n   <span class=\"hljs-string\">\"John\"<\/span>\r\n]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example uses the <code>json_encode()<\/code> function to convert an associative array in PHP to an object in JSON:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$person = &#91;\n    <span class=\"hljs-string\">'name'<\/span> =&gt; <span class=\"hljs-string\">'Alice'<\/span>,\n    <span class=\"hljs-string\">'age'<\/span> =&gt; <span class=\"hljs-number\">20<\/span>\n];\n\nheader(<span class=\"hljs-string\">'Content-type:application\/json'<\/span>);\n<span class=\"hljs-keyword\">echo<\/span> json_encode($person);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">{\r\n   name: <span class=\"hljs-string\">\"Alice\"<\/span>,\r\n   age: <span class=\"hljs-number\">20<\/span>\r\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In practice, you would <a href=\"https:\/\/phptutorial.net\/php-pdo\/php-pdo-select\/\">select data from a database<\/a> and use the <code>json_encode()<\/code> function to convert it to the JSON data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='converting-json-data-to-php-variables'>Converting JSON data to PHP variables <a href=\"#converting-json-data-to-php-variables\" class=\"anchor\" id=\"converting-json-data-to-php-variables\" title=\"Anchor for Converting JSON data to PHP variables\">#<\/a><\/h2>\n\n\n\n<p>To convert JSON data to a variable in PHP, you use the <code>json_decode()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">json_decode ( string $json , bool|<span class=\"hljs-keyword\">null<\/span> $associative = <span class=\"hljs-keyword\">null<\/span> , int $depth = <span class=\"hljs-number\">512<\/span> , int $flags = <span class=\"hljs-number\">0<\/span> ) : mixed<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example shows how to use <code>json_decode()<\/code> function to convert JSON data to a variable in PHP:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$json_data = <span class=\"hljs-string\">'{\"name\":\"Alice\",\"age\":20}'<\/span>;\n\n$person = json_decode($json_data);\n\nvar_dump($person);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">object(stdClass)<span class=\"hljs-comment\">#1 (2) {<\/span>\n    &#91;<span class=\"hljs-string\">\"name\"<\/span>] =&gt; string(<span class=\"hljs-number\">5<\/span>) <span class=\"hljs-string\">\"Alice\"<\/span>     \n    &#91;<span class=\"hljs-string\">\"age\"<\/span>] =&gt; int(<span class=\"hljs-number\">20<\/span>)\n}    <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>json_decode()<\/code> function converts an object in JSON to an object in PHP. The object is an instance of the <code>stdClass<\/code> class. To convert JSON data to an object of a specific class, you need to manually map the JSON key\/value pairs to object properties. Or you can use a third-party package.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='serializing-php-objects'>Serializing PHP objects <a href=\"#serializing-php-objects\" class=\"anchor\" id=\"serializing-php-objects\" title=\"Anchor for Serializing PHP objects\">#<\/a><\/h2>\n\n\n\n<p>To serialize an object to JSON data, you need to implement the <code>JsonSerializable<\/code> interface. The <code>JsonSerializable<\/code> interface has the <code>jsonSerialize()<\/code> method that specifies the JSON representation of the object.<\/p>\n\n\n\n<p>For example, the following shows how to implement the <code>JsonSerializable<\/code> interface and use the <code>json_encode()<\/code> function to serialize the object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span> <span class=\"hljs-keyword\">implements<\/span> <span class=\"hljs-title\">JsonSerializable<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">private<\/span> $name;\n\n    <span class=\"hljs-keyword\">private<\/span> $age;\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">(string $name, int $age)<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">$this<\/span>-&gt;name = $name;\n        <span class=\"hljs-keyword\">$this<\/span>-&gt;age = $age;\n    }\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">jsonSerialize<\/span><span class=\"hljs-params\">()<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">return<\/span> &#91;\n            <span class=\"hljs-string\">'name'<\/span> =&gt; <span class=\"hljs-keyword\">$this<\/span>-&gt;name,\n            <span class=\"hljs-string\">'age'<\/span> =&gt; <span class=\"hljs-keyword\">$this<\/span>-&gt;age\n        ];\n    }\n}\n\n<span class=\"hljs-comment\">\/\/ serialize object to json<\/span>\n$alice = <span class=\"hljs-keyword\">new<\/span> Person(<span class=\"hljs-string\">'Alice'<\/span>, <span class=\"hljs-number\">20<\/span>);\n<span class=\"hljs-keyword\">echo<\/span> json_encode($alice);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">{<span class=\"hljs-string\">\"name\"<\/span>:<span class=\"hljs-string\">\"Alice\"<\/span>,<span class=\"hljs-string\">\"age\"<\/span>:<span class=\"hljs-number\">20<\/span>}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, define a Person class that implements the JsonSerializable interface.<\/li><li>Second, return an array that consists of name and age properties from the <code>jsonSerialize()<\/code> method. The <code>json_encode()<\/code> function will use the return value of this method to create JSON data.<\/li><li>Third, create a new <code>Person<\/code> object and serialize it to JSON data using the <code>json_encode()<\/code> function.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>JSON is a lightweight data-interchange format.<\/li><li>Use the <code>json_encode()<\/code> function to convert PHP variables to JSON.<\/li><li>Use the <code>json_decode()<\/code> function to convert JSON data to PHP variables.<\/li><li>Implement the <code>JsonSerializable<\/code> interface to specify the JSON representation of an object.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1409\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-json\/\"\n\t\t\t\tdata-post-title=\"PHP JSON\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1409\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-json\/\"\n\t\t\t\tdata-post-title=\"PHP JSON\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to manipulate JSON in PHP using the json_encode, json_decode, and JsonSerializable interface.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1753,"menu_order":35,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1409","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1409","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=1409"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1409\/revisions"}],"predecessor-version":[{"id":2339,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1409\/revisions\/2339"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1753"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=1409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}