json_decode

json_decode:- It is used for decoding JSON in PHP. This function returns the value decoded from json to appropriate PHP type.

Syntax:-
mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

Parameters:-
(i) json :- The json string being decoded. This function only works with UTF-8 encoded data.
(ii) assoc:- When TRUE, returned objects will be converted into associative arrays.
(iii) depth:- User specified recursion depth.
(iv) options:- Bitmask of JSON decode options. Currently only JSON_BIGINT_AS_STRING is supported (default is to cast large integers as floats).

Example:-
[php]
<?php
$jsonData = ‘{ "user":"John", "age":26, "country":"India" }’;
$phpArray = json_decode($jsonData);
print_r($phpArray);
foreach ($phpArray as $key => $value) {
echo $key .’|’. $value";
}
?>[/php]
Output:- stdClass Object ( [user] => John [age] => 26 [country] => India )
Output:- user | John
Output:- age | 26
Output:- country | India

json_encode

json_encode:- Returns the JSON representation of a value.

Syntax:-
string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )

Parameters:-
(i) value:- The value being encoded. This function only works with UTF-8 encoded data.
(ii) options:- This optional value is a 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
(iii) depth:- Set the maximum depth. Must be greater than zero.

example 1:- PHP arrays to JSON strings.
[php]
<?php
$company_name_ar = array(‘TCS’, ‘Infosys’, ‘HCL’, ‘Wipro’);
echo json_encode($company_name_ar);
?>
[/php]
Output:- [“TCS”,”Infosys”,”HCL”,”Wipro”]

JSON value pass into javascript.
[php]
<script type="text/javascript">
var company_name = <?php echo json_encode($company_name_ar);?>
document.write(company_name);
document.write(company_name[0]);
document.write(company_name[1]);
document.write(company_name[2]);
document.write(company_name[3]);
</script>[/php]
Output:- TCS,Infosys,HCL,Wipro
Output:- TCS
Output:- Infosys
Output:- HCL
Output:- Wipro

example 2:- JSON value pass into javascript.
[php]
<?php $company_ar = array("name"=>"TCS", "location"=>"Noida"); ?>
<script type="text/javascript">
var company= <?php echo json_encode($company_ar); ?>
document.write(company.name);
document.write(company[‘name’]);
document.write(company.location);
document.write(company[‘location’]);
</script>[/php]
Output:- TCS
Output:- TCS
Output:- Noida
Output:- Noida

JSON with Php

When you want to pass data from php to javascript or javascript to php then we use JSON.

JSON Enviorment:-
As of PHP 5.2.0, the JSON extension is bundled and compiled into PHP by default.

JSON Functions:-
(i) json_encode:- Returns the JSON representation of a value.
(ii) json_decode:- Decodes a JSON string.
(iii) json_last_error_msg:- Returns the error string of the last json_encode() or json_decode() call.
(iv) json_last_error:- Returns the last error occurred.

JSON data type value

JSON value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.
JSON values

How to create a JSON Array?

An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
JSON Array
(i) Empty Array:- that array has not value is called Empty Array.Empty Array always return, undefine value.
[php]
<script>
var myarr=[];
document.write(myarr[0]); //Output:- undefined
</script>
[/php]
(ii) Array of strings:- In the below eg. array has 3 string values. array index start 0.
[php]
<script>
var myarr=["John","Tony","Alix"];
document.write(myarr[0]); //Output:- John
document.write(myarr[1]); //Output:- Tony
document.write(myarr[2]); //Output:- Alix
</script>
[/php]
(iii) Array of Numbers:- In the below eg. array has 3 number values.
[php]
<script>
var myarr=[11,12,13];
document.write(myarr[0]); //Output:- 11
document.write(myarr[1]); //Output:- 12
document.write(myarr[2]); //Output:- 13
</script>
[/php]
(iv) Array of Booleans :- In the below eg. array has 3 boolean values.
[php]
<script>
var myarr=[true,false,true];
document.write(myarr[0]); //Output:- true
document.write(myarr[1]); //Output:- false
document.write(myarr[2]); //Output:- true
</script>
[/php]
(v) Array of Array :- In the below eg. array has 2 array.
[php]
<script>var myarr=[
["a","b","c"],["x","y","z"]
];
document.write(myarr[0][0]); //Output:- a
document.write(myarr[0][1]); //Output:- b
document.write(myarr[0][2]); //Output:- c
document.write(myarr[1][0]); //Output:- x
document.write(myarr[1][1]); //Output:- y
document.write(myarr[1][2]); //Output:- z
</script>
[/php]
(vi) Array of Objects:- In the below eg. array has 2 object values.
[php]
<script>
var myarr_data = [ {}, {}, {}, {}, {} ];
myarr=[{name:"john",age:27},{name:"tony",age:28}]
document.write(myarr[0].name); //Output:- john
document.write(myarr[0].age); //Output:- 27
document.write(myarr[1].name); //Output:- tony
document.write(myarr[1].age); //Output:- 28
</script>
[/php]

(vii) Array with Mixed Data Types:-
[php]
<script>
var myarr_data = [ {}, "text",11,null,true,[] ];
</script>
[/php]

How to create a JSON Object

An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
object_json

Note:- JSON values can be any JSON data type: string, number,boolean, object, array, null.

How to access JSON Object Values?
Object values can be accessed like an array using an index, using the property name in quotes, or by following the object’s variable name with a period and the property name.
Example:-
[php]
<script type="text/javascript">
var empObject = {
"name": "John",
"age": 27
};
</script>[/php]

Access name and age value with array notation
[php]
<script type="text/javascript">
document.write(empObject[‘name’]);
document.write(empObject[‘age’]);
</script>[/php]
Output:- John 27

Access name and age value with dot notation
[php]
<script type="text/javascript">
document.write(empObject.name);
document.write(empObject.age);
</script>
[/php]
Output:- John 27

Note:- Property value may be override, if you define the value of property again.
[php]
<script type="text/javascript">
empObject.name="Tony";
document.write(empObject.name);
</script>[/php]
//Output:- Tony

why use json?

(i) These structures are supported, all modern programming languages.
(ii) The JSON standard is language-independent and its data structures, arrays and objects, are universally recognized.
(iii) These qualities make it an ideal format for data interchange on the web.
(iv) JSON is easily readable than xml.
(v) JSON is fast than xml.
So, now Web Services and API.s use JSON format to provide public data like facebook, twitter etc.

What is json?

JSON (JavaScript Object Notation) is a lightweight data-interchange format.
JSON is easy for humans to read and write.
JSON is based on a subset of the JavaScript Programming Language.
JSON is a text format that is completely language independent.