JSON Tutorial: Learn JSON with Simple Examples

JSON Tutorial for Beginners.

What is JSON?

The JSON full form is JavaScript Object Notation. JSON data format is a language-independent standard data interchange format. It is an alternative to other data formats like XML or CSV. It can be read and understood by humans as well as computer devices. Douglas Crockford created the JSON format. JSON Data A JSON data value may be a string, which is a sequence of characters. Or a number, which may be an integer or floating-point Or an object, which is a set of name-value pairs. Or JSON array, which is a set of comma-separated values. Or boolean meaning true or false or Null, which is an empty value. Comments are not allowed in JSON. A JSON data value can even nest other values within it.

Now let us learn about each of these with JSON data examples.

The first JSON data type is a string. It is a sequence of characters. The string is delimited by double quotes on both sides. JSON strings support the backslash escape sequence A JSON example of string is “name”: “John” and you can see both names as well as John, both strings are delimited by the double quotes on both sides. Another example is “category”: “employee” Second JSON data type is number. A number in JSON can be integer or floating-point. A floating-point number can even include the exponential notation, that is useful to show very small or very large numbers. A sample JSON number is “age”:30 and here you can see that since age is a string, it is delimited by double-quotes.

Another JSON number example is “id”:123 and another example is “version”:1.1 1.1 is a floating-point number. The third JSON data structure is an object. An object in JSON is an unordered set of name-value pairs. In different programming languages, such a data structure is also known as a record, a dictionary or an associative array. In a JSON object, names are strings. The name is separated with a colon from its value. A JSON object starts with a left curly brace and it ends with a right curly brace. Now, let us see a JSON data example of an object with name-value pairs. So, here you can see that it is the From part of an email and email’s from address is shown as a JSON object. It starts with a left curly brace and it ends with a right curly brace. It has three name-value pairs. for address, domain and safe sender.

The fourth JSON data structure is the JSON array. A JSON array is a set of comma-separated values. In different programming languages, it is also known as a list or a sequence. In JSON, an array starts with the left square bracket and it ends with the right square bracket. The data values within the array are comma-separated. JSON data example of an array is like names of different models of cars. So, it starts with the left square bracket, and it ends with the right square bracket and within it, the values which are strings, are separated by commas. Another data example of an array is an array of integers. Again, it starts with the left square bracket, and it ends with the right square bracket and here the values are integers, which are again separated by commas.

The fifth JSON data type is boolean. A JSON data value of boolean type can be true or false. A JSON example of boolean is “isCustomer”: true Another JSON example of boolean is “closed”: false Sixth JSON data type is null. A JSON null value is an empty data value. A JSON data example of null is “spouse”: null meaning that the person does not have a spouse or “vehicle”: null Now, let us talk a bit about JSON fie. The JSON file is a text file. This means that you can create JSON file in any text editor like Notepad++ or even Notepad. The JSON file extension is .json

Now, how to create JSON file? There are many JSON generators. So let us see examples of those. Here is the example of a JSON generator. So, here you can see a number of values – index, index start at, integer, float, name and you can specify your own values and you can specify how many numbers of rows you want to generate? So, let us say we want to generate three rows and click on the Generate button. And it creates a JSON object for us. So this is the items array and within items, we can see that there are three different objects. You can also create a JSON file using JavaScript programming. Now, let us talk about sample JSON data. Sample JSON data is available at the JSON official website, which is JSON.org and here you can see many examples.