Decode JSON data with PHP


JSON, short for JavaScript Object Notation, is a lightweight computer data interchange format. It is a text-based, human-readable format for representing simple data structures and associative arrays (called objects). If you are running your web server with latest PHP package, then it is so easy to decode JSON data with an in-built function of PHP. json_decode() function decodes a json string. It is available in PHP version 5.2 and later.

An Example:


Output of the above code will look like this:

object(stdClass)#1 (5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}
array(5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

Read more about JSON:

About SachinKRaj

Sachin is a web application developer, technology blogger and web addict! He has over 6 years of web development experience and he writes tutorials primarily focused on LAMP, Ajax, Api's, jQuery etc. He is usability expert and he always likes to share his knowledge with people.

, , , ,

3 Responses to Decode JSON data with PHP

  1. Prashant August 8, 2009 at 10:43 pm #

    Thanks Sachin, this helps me a lot in creating import script for MochiMedia.com game feed, because they provides data in JSON format and this function is the perfect for converting JSON data to an array and then running loop on that array, to use the data. :D

  2. SachinKRaj August 8, 2009 at 10:58 pm #

    I am glad that it helped you. It is one of the best feature of PHP, that you get almost everything with hundreds of internal (built-in) methods and ready to use. No further coding required. ;) :D

  3. Shubhamoy August 9, 2009 at 11:43 pm #

    Your posts are really very cool! Though I haven’t used JSON yet but at least learned about it :)

Leave a Reply