How to parse different JSON formats using jQuery/JavaScript

  1. Previous
  2. Next
  3. Recent

    1. Signs 'He' May be Cheating on You
    2. 7 Vows of Hindu Marriage - Know What 'Saat Vachans' Mean
    3. RuPay vs Visa vs Mastercard vs American Express- A Comparison
    4. What is RuPay? Everything You Need to Know About India's Domestic Payment System!
    5. Top 10 Online Portals to Find a High Paying Job in India
    6. Top Indians who Revolutionized the IT Sector in India
    7. 8 Initiatives by Narendra Modi that Could Change the Future of the Nation
    8. 8 Work-from-home Jobs for Housewives, Students, and Part timers
    9. Complete Company Registration Process in India - Explained
    10. Change Name in India in 5 Steps - Filing Affidavit, Newspaper Ad & Gazette Notification

JSON, developed by Douglas Crockford helps in easy storing and transportation of data. Its much like XML but much faster and lightweight. Parsing JSON is easier compared to XML.

JSON files are available in different formats. In the tutorial, we’ll learn about parsing different JSON formats. We’ll also give an insight of the syntax of JSON files which will help in understanding the parsing much better.

1. Parsing JSON format 1

The very basic JSON file contains only name-value pairs. They start with ‘{‘ and end by ‘}’ mark. A simple JSON file is {“name”:”CyberPassion”, “age”:”10 months”}

<script language="Javascript">

var JSON_simple={"name":"CyberPassion", "age":"10 months"};
alert(JSON_simple.name); //Alerts 'CyberPassion'
alert(JSON_simple.age); //Alerts '10 months'

</script>

2. Parsing JSON format 2

This format has one or more name-value pairs inside an array. They start by ‘[‘ and ends by ‘]’ just like a normal array. A JSON data of such format is [{“name”:”CyberPassion”, “age”:”10 months”}]

<script language="Javascript">

var JSON_arrayonly=[{"name":"CyberPassion", "age":"10 months"}];
alert(JSON_arrayonly[0].name); //Alerts 'CyberPassion'
alert(JSON_arrayonly[0].age); //Alerts '10 months'

</script>

3. Parsing JSON format 3

Most JSON files have arrays within name-value pairs. They look similar to this {“blog”: [{“name”:”CyberPassion”, “age”:”10 months”}]}

<script language="Javascript">

var JSON_complex={"blog":
[{"name":"CyberPassion", "age":"10 months"},
{"name":"iyaara", "age":"3 months"}]
};
alert(JSON_complex.blog[0].name); //Alerts 'CyberPassion'
alert(JSON_complex.blog[1].name); //Alerts 'iyaara'

</script>
blog comments powered by Disqus