Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Handling JSON with PHP

Monday, September 12th, 2016 by Servage

jsonJSON is a data serialization format that has become the de-facto standard for transmitting data between applications. It is more and more often preferred over its competitor XML thanks to being lightweight, human-readable and widely supported. JSON is also supported by PHP, so let’s see how PHP can be used to read and write JSON.

Creating a JSON Object

PHP has two functions that are often used to deal with JSON: json_encode() and json_decode(). These functions have been built in to PHP since 5.2.0. First, let’s see how we can use json_encode to create a new JSON object. Before we get started, it’s good to know what kind of data can be turned into JSON. The answer is rather simple: any types except resources. This means you can convert integers, booleans, strings, arrays and much more into JSON.

One of the most common uses for json_encode() is to turn an associative array into JSON. You can do so by calling the json_encode() function and passing an associative array as the first argument, like this:

json_encode([“key1” => “value1”, “key2” => “value2”]);

This will result in a JSON object that looks like {“key1″:”value1″,”key2″:”value2″}.

You can also pass some extra options to the function as the second argument, but most of the time you only need to use the first argument. Converting an array into JSON can be useful in many situations, for example in API applications. You can fetch data from a database into an array or object. You can then convert the data into JSON and return it when someone sends a query to your API. If you were reading carefully, you probably noticed the word ”object”. That’s right, you can also convert certain objects into JSON, although it’s not as simple as converting an array.

Reading JSON Data

Next let’s have a look at the second function called json_decode(). As the name suggests, it’s the opposite of json_encode() and can be used to convert JSON-formatted strings back to a format that PHP can understand. A JSON-formatted string is what you get when you convert something into JSON using json_encode(). It’s a regular PHP string that contains key-value pairs using the JSON syntax.

To show an example, let’s json_decode() the previously generated JSON object:


You might be thinking that this would return back an associative array. However, it returns a standard PHP object. This is not a problem though, since you can use a foreach loop to loop through the keys and values. If you want the data in an array, you convert the data into an array inside the foreach loop. Using something along the lines of foreach($item as $key => $value) should get you started with that.

Handling JSON with PHP, 4.2 out of 5 based on 5 ratings
Categories: Guides & Tutorials

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.