Looping through JSON

0
1973

We have gone through the theory of JSON in the previous session. Today we will learn how to create JSON objects and arrays practically and how to traverse through the arrays using loops.
Let’s start the task step by step.

  1. Create a new folder on desktop and name it as “Sec4_Ch21”.
  2. Open a new notepad++ document and save it as “index.html” in the newly created folder “Sec4_Ch21”.
  3. Write the following code in “index.html” file.
  4. When we open the file “index.html” with any browser say chrome, it will look like this:

    initial_output
    fig 1

  5. In this tutorial of Looping through JSON, we are going to use the same information of movies used in XML file in last session but in JSON format. To store information JSON objects are used, so create a JSON object in the head section of the document in the tags as follows:
  6. In the above code, movie is the object name that contains the information about a movie. Information of an object is always written in the curly braces and it is in the form of name/value pair separated by comma (,).
  7. Now to grab this information and display it on the screen, write the following code in the body section of the document inside
    tags.
    • Here, title of the movie is displayed with the statement
      where, movie is the object name and title is its property. And genre of the movie is displayed with the statement where, movie is the object name and genre is its property.
    • br tag is used for a line brake.
    • Hence any information in the object is displayed with object name with its property along with a period (.) in between.
    • The statement is used to display any text.
    • Output of the above code is shown below:
    • displaying_JSON_object_info
      fig 2

    • The Godfather is the title of the movie and Drama is its genre.
      This is how we access a JSON object.
  8. If we want some more objects i.e. if we want to store the information of some more movies we can create that many objects but it is not a good practice. To store information in more than two or three objects we can directly declare an array.
    • Let us create an array to store the information of five movies.
    • Write the following code in the head section of the document in tags:
    • Here, we have stored information of 5 movies in the array named movies.
    • Each array value in the above case is a movie object separated by comma.
    • The objects in the array are contained in the opening and closing square brackets [].
  9. Now let us print some of the array values. Write the following code in body section inside the
    tags.
    • Here, we have printed the values using document.write(); statement.
    • Looking at the statements we can understand that we are trying to print the genre of first movie and the title of the third movie.
    • Since we are dealing with arrays, we know that data is stored in array from index 0 (zero).
    • So while printing the value, the array name with the index of the object in square brackets followed by a particular property with a period in between is given.
    • The output of the code above is shown below:
    • displaying_JSON_array_info
      fig 3

    • In the above output, the genre of first movie and the title of the third movie are displayed.
  10. We have displayed just two values in the above code, but if we want to display the whole array, we cannot just sit and write code for each and every value. We can use any looping structure for this.
    • Let us write the following code to display the whole array using for loop in the body section:
    • In the above code, the code to display array values is written in a div block given an id container.
    • A heading My favourite Movies is given in h1 tag.
    • The movies details are displayed in an un-ordered list.
    • tag is defined inside tags because everything before it, was html and now we want to write the scripting code.
    • So we have used for loop to display the array elements.
    • Condition given in for loop is . Here we know “movies” is an array and array is an object. Objects have properties and hence length is the property of an array which gives the number of elements present in the array. In the above example of movies, length property will return value 5, since we have 5 movie objects in the array movies and for loop will therefore iterate 5 times.
    • We are going to display the movie details in an un-ordered list with all the movie titles as the heading and the other details of the movie under it as the list items.
    • Title of every movie is displayed with the h4 heading tag in the statement

      Here, h4 tag is the html part hence written in the string format and concatenated with the code to display movie title i.e. movies[i].title. The variable i will change accordingly from 0 to 5.
    • The output after displaying only movie titles is shown below:
    • displaying_movie_titles_in_for_loop
      fig 4

    • Now, when we write the further statements in the for loop i.e.

      It will display the year, genre and director under the title of each movie. Since these are the list items of un-ordered list, they are enclosed in the li tags.

    • The output after writing the whole code is seen like this:
    • displaying_movie_details_in_for_loop
      fig 5

    • All the 5 movie details are shown in an un-ordered list in the above output. But this displayed information needs some styling to look presentable.
  11. Let us provide some styling to the page.
    • We have defined a div block with id container in the above given code; this will be used for styling.
    • Write the following code in head section of the page below the page title and outside the script tag:
    • The page after styling will look like this:
    • Array_elements_after_styling
      fig 6

    • Let us understand, how styling is done?
    • We have used inline styling by writing the code for it in the

      tags in the head section.
    • First the Heading “My Favourite Movies” heading is styled. Since this heading is written in tags, h1 tag is styled. The code for it is shown below:
    • Here, Due to h1 tag the size of the heading is already large, so we have just placed the heading text at the center of the page by using the statement text-align:center;
    • Next, we have styled the h4 tag that contains the titles of the movies. The code is shown below:
    • Here, each movie title is given no margin i.e. margin:0; padding of 5 pixels is given from all sides and a grey background color is given using the statement background:#f4f4f4;
    • Next the list items i.e. the movie details are styled using the following code:
    • The list items have bullets due un-ordered list, these bullets are removed using the statement list-style:none; padding of 5 pixels is given from the left side using statement padding-left:5px;
    • The div block having the id container is also styled with the following code:
    • Since container is an id of the div block, it is preceded with the # sign. The div block is given a width of 600 pixels. Margin is given auto; this auto indicates that all the content should be placed in the middle of the page. Any overflow or distortion is hidden using the statement overflow:hidden;

Thus we successfully finished working with Looping through JSON.

LEAVE A REPLY

Please enter your comment!
Please enter your name here