JSON, which stands for JavaScript Object Notation, is a widely used data interchange format. It is primarily used to transmit data between a server and web application as an alternative to XML.
JSON is known for its simplicity, readability, and ease of use. In this article, we will explore the data types that JSON supports and how they are represented.
Primitives:
JSON supports six primitive data types: string, number, boolean, null, object, and array. Let’s take a closer look at each of them.
String:
A string is a sequence of Unicode characters enclosed within double quotation marks. It can contain any valid Unicode character except for control characters (e.g., newline or tab). Here’s an example:
“Hello World!”
Number:
A number in JSON can be written with or without decimal points. It can also be written in scientific notation.
JSON numbers do not differentiate between integers and floating-point values. Here are some examples:
42
3.14
-0.27e2
Boolean:
A boolean represents either true or false. It is case-sensitive and should be written in lowercase letters only.
true
false
Null:
The null value represents an empty value or absence of any object value.
null
Complex Data Types:
In addition to the primitive types mentioned above, JSON also supports two complex data types: objects and arrays.
Object:
An object in JSON is an unordered collection of key-value pairs enclosed within curly braces {}. Each key-value pair consists of a property name (in double quotation marks) followed by a colon and the corresponding value. Multiple key-value pairs are separated by commas.
Here’s an example of a JSON object:
{ “name”: “John Doe”, “age”: 30, “city”: “New York” }
Array:
An array is an ordered collection of values enclosed within square brackets []. Values within an array can be of any JSON data type, including objects and arrays themselves. Multiple values are separated by commas.
Here’s an example of a JSON array:
[ “apple”, “banana”, “orange” ]
Summary:
In summary, JSON is a data interchange format that supports six primitive data types: string, number, boolean, null, object, and array. Understanding these data types is crucial when working with JSON in web development.
Now that you have a good grasp of the different data types supported by JSON, you can effectively use them to represent and exchange data between your server and web application.