How to get json object value in php

I have an url passing parameters use json_encode each values like follow:

$json = array
(
    'countryId' => $_GET['CountryId'],
    'productId' => $_GET['ProductId'],
    'status'    => $_GET['ProductId'],
    'opId'      => $_GET['OpId']
);

echo json_encode($json);

It's returned a result as:

{  
  "countryId":"84",
  "productId":"1",
  "status":"0",
  "opId":"134"
}

Can I use json_decode to parse each values for further data processing?

Thanks.

How to get json object value in php

George G

7,13412 gold badges42 silver badges58 bronze badges

asked Sep 14, 2012 at 17:08

How to get json object value in php

2

json_decode() will return an object or array if second value it's true:

$json = '{"countryId":"84","productId":"1","status":"0","opId":"134"}';
$json = json_decode($json, true);
echo $json['countryId'];
echo $json['productId'];
echo $json['status'];
echo $json['opId'];

answered Sep 14, 2012 at 17:12

How to get json object value in php

Mihai IorgaMihai Iorga

38.6k15 gold badges107 silver badges106 bronze badges

4

json_decode will return the same array that was originally encoded. For instanse, if you

$array = json_decode($json, true);
echo $array['countryId'];

OR

$obj= json_decode($json);

echo $obj->countryId;

These both will echo 84. I think json_encode and json_decode function names are self-explanatory...

answered Sep 14, 2012 at 17:14

How to get json object value in php

Vladimir HrabanVladimir Hraban

3,4333 gold badges24 silver badges43 bronze badges

0


What is JSON?

JSON stands for JavaScript Object Notation, and is a syntax for storing and exchanging data.

Since the JSON format is a text-based format, it can easily be sent to and from a server, and used as a data format by any programming language.


PHP and JSON

PHP has some built-in functions to handle JSON.

First, we will look at the following two functions:

  • json_encode()
  • json_decode()

PHP - json_encode()

The json_encode() function is used to encode a value to JSON format.

Example

This example shows how to encode an associative array into a JSON object:

$age = array("Peter"=>35, "Ben"=>37, "Joe"=>43);

echo json_encode($age);
?>

Run Example »

Example

This example shows how to encode an indexed array into a JSON array:

$cars = array("Volvo", "BMW", "Toyota");

echo json_encode($cars);
?>

Run Example »



PHP - json_decode()

The json_decode() function is used to decode a JSON object into a PHP object or an associative array.

Example

This example decodes JSON data into a PHP object:

$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';

var_dump(json_decode($jsonobj));
?>

Run Example »

The json_decode() function returns an object by default. The json_decode() function has a second parameter, and when set to true, JSON objects are decoded into associative arrays.

Example

This example decodes JSON data into a PHP associative array:

$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';

var_dump(json_decode($jsonobj, true));
?>

Run Example »


PHP - Accessing the Decoded Values

Here are two examples of how to access the decoded values from an object and from an associative array:

Example

This example shows how to access the values from a PHP object:

$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';

$obj = json_decode($jsonobj);

echo $obj->Peter;
echo $obj->Ben;
echo $obj->Joe;
?>

Run Example »

Example

This example shows how to access the values from a PHP associative array:

$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';

$arr = json_decode($jsonobj, true);

echo $arr["Peter"];
echo $arr["Ben"];
echo $arr["Joe"];
?>

Run Example »


PHP - Looping Through the Values

You can also loop through the values with a foreach() loop:

Example

This example shows how to loop through the values of a PHP object:

$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';

$obj = json_decode($jsonobj);

foreach($obj as $key => $value) {
  echo $key . " => " . $value . "
";
}
?>

Run Example »

Example

This example shows how to loop through the values of a PHP associative array:

$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';

$arr = json_decode($jsonobj, true);

foreach($arr as $key => $value) {
  echo $key . " => " . $value . "
";
}
?>

Run Example »



How to get value of JSON in PHP?

To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.

How can access JSON decoded data in PHP?

PHP - Accessing the Decoded Values $obj = json_decode($jsonobj);

What is File_get_contents PHP input?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

What is JSON decode in PHP?

The json_decode() function is used to decode or convert a JSON object to a PHP object.