How to Use PHP to Parse JSON

Written by serm murmson
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Use PHP to Parse JSON
You can use PHP to efficiently manage JSON data (Hemera Technologies/ Images)

JavaScript Object Notation (JSON) is an efficient way of integrating JavaScript with the storage and transportation of data. Hypertext preprocessor (PHP) is well-suited to unpacking and processing the data stored in a JSON file or string; in fact, there are special functions in PHP that are specially designed to both encode and decode JSON data. Once this data has been decoded, there are many ways to display it or process it through a manipulation of PHP variables.

Skill level:

Things you need

  • Text editor

Show MoreHide


  1. 1

    Open the file that will house your PHP script.

  2. 2

    Begin the PHP script with the "<?php" tag. On the next line, use the "json_decode()" function, which will decode the JSON data. Set a variable in which to store your decoded JSON data, and direct the "json_decode" function to the desired data. For example:

    $decode = json_decode($json);

  3. 3

    Use the PHP echo function to display different data from your decoded JSON variable. For example, if the data is an array of information about different types of animals and their diets:

    echo $decode->animal[0]-> food;

    To cycle through different values, change the number after animal to the appropriate index number (note that the first index number, unless otherwise specified, will be 0).

  4. 4

    Use a "for" or "foreach" loop to process multiple different values of your data quickly. A "for" loop ends once the specified condition is met. For example, to increment the index numbers by one and cycle through the first 5 values:


    echo $decode->animal[$i]->food . " ";


    With the variable "$i" as the index number, the "$i++" command will increment $i by one each time the loop finishes. The loop will begin at $i=0 and continue as long as $i<5.

    A foreach loop will cycle through all values of the specified data parameter. For example:

    foreach($decode->animal->food as $food){

    echo $food . " ";


Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.