Servage Magazine

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

Using JSON data from PHP can be tricky

Sunday, August 5th, 2012 by Servage

AJAX is a common thing, used on almost every website today. Mainly it’s describing the concept of communicating with the server in the background, or without updating the page, to send and get data. This enables you to build nice, modern and response sites and apps.

Many AJAX calls work with JSON as the preferred data format. JSON is like a JavaScript object, which makes it very convenient to work with. Also most languages easily convert from own datatypes to JSON.

In PHP there is the possibility to json_encode() and json_decode() which transforms a PHP variable (array, object, anything except a resource) into a JSON string, which can be used by the client JavaScript. See the PHP manual for details.

I often use PHP arrays to build my dataset serverside, and convert that to JSON, before sending it to the client. This is a nice and clean way to do it, but unfortunately there are a few tricky caveats to be aware of.

Array vs. Object

PHP can handle arrays with numeric keys that are out of order, and even jump over certain values. Example keys: 1,9,3,12 each hold a string value. If you convert this JSON using json_encode() then the resulting JSON will not contain an array, but an object, with properties called 1,9,3,12 (like you keys). This might be unwanted behavior, so make sure you have proper numeric keys, in order, in your PHP array, if you want to results JSON to include an array, not an object.

String vs. Integer

PHP developers are lazy people, so we love type juggling, which basically means, that PHP isn’t strictly throwing errors if we want to use a string for a calculation, or display a number as a string. So, if you’re getting an expected value from a database for example, which is a number, you will actually receive it as a string from MySQL. If you try to make a calculation, then PHP with automatically try to interpret the string as a number. Therefore datatypes are often not so important to have correct. However, if you use json_encode() to convert such data to JSON, then the datatypes will be preserved where possible. In that case JavaScript will reveice a string with a number inside, which it cannot use for calculations. Then you need to convert the string to a number datatype in JavaScript first. This is annoying.

So, be awake when working with JSON in JavaScript which originates from a source using json_encode(), there might be unexpected behaviour ;-)

Using JSON data from PHP can be tricky, 4.1 out of 5 based on 7 ratings
Categories: Tips & Tricks

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.