How to convert JSON format to Object variable using PHP

by Oozman

in PHP Tutorials

If you’re working on Ajax or in an inter-operable web applications, you will find that using files that are accessible through HTTP or your web browser will be a useful solution in communicating and accessing data from each platform. Some of these files are in the form of simple text file, XML file, or even a PHP file that returns JSON or XML formatted strings.

Today, we will focus how to convert and use JSON formatted string to an Object variable in PHP. Also I would like to let you know that JSON is also one of the famous format in doing inter-operable web application, specially on Ajax solutions.

So here is how I can use/convert a JSON to Object (PHP) variable in my web applications. Actually, its very simple and straightforward.

Since PHP 5+ had recognized JSON format, they’ve actually built functions right inside PHP that handle this type of file format. Because of this, manipulating JSON formats was made easy.

Here are the three important functions that you will be familiar with in terms of managing your JSON files in your PHP web applications.

  • file_get_contents() → this is a function that will get a content from a file. Basically, it’s a content in browser’s point of view.
  • json_encode() → this is the function that will transform an Array data into a JSON formatted string.
  • json_decode() → this is the function that will transform a JSON formatted string into an Array or Object.

Once you get the functions required, you will be set to go.

Let’s get started:

First, create a file name json.php and insert these following codes:

<?php
//create an array of data
$data = array('name1'=>'One',
              'name2' => 'Two',
              'name3' => 'Three',
              'name4' => 'Four');

//convert array to JSON and print it
echo json_encode($data);

?>

These codes basically, creates an Array of data and then convert this Array into a JSON format, then finally prints.

Second, create another file called json_caller.php and insert these following codes:

<?php
//get contents of a file with JSON formatted string
$data = file_get_contents("http://localhost/json.php");

//convert JSON to Object and store it to a variable
$json_to_object_php = json_decode($data);
//$json_to_object_php = json_decode($data, true);

//dump or examine converted JSON to Object variable
var_dump($json_to_object_php);

?>

Basically we get the contents of json.php through HTTP GET method. In other words, what ever contents that this file returned in the browser, it will get it and store it to a variable. Next, it will transform or decode this data into an Object. Finally, it will dump the converted data as an Object variable.

Now upload everything to your “localhost” and access, http://localhost/json_caller.php. You will be expected to see a string describing an Object variable. Like this screenshot below:


Notice also in json_caller.php that we add “true” as a parameter in decoding a data (string). This is because, json_decode() function accepts an optional parameter called “assoc”, in which if set to true, instead of returning an Object value, it will return an Associative Array.

The only key here to achieve and master this technique is to understand those three methods that I’ve introduced to you. They are very handy and to be able to understand them will add something on your skill set in terms of building Ajax or inter-operable web applications in PHP.

So, there you have it. I hope you get something out of it. If you have any questions, feel free to leave a comment.

{ 4 comments… read them below or add one }

Zed March 15, 2011 at 6:05 am

Many thanks!

Reply

Joselito November 29, 2011 at 9:17 pm

LOOOOSER!!

Reply

Joselito February 28, 2012 at 8:11 am

loserjito!

Reply

Fabian October 30, 2012 at 2:50 pm

Thanks

Reply

Leave a Comment

Previous post:

Next post: