While discussing the JSON syntax in the previous section, we glanced at some of the basic data types used in JSON. Let's take a detailed look at each item.
Here is the list of the basic data types available with JSON:
- Number: This type is used for storing a signed decimal number that may optionally contain a fractional part. Both integer and floating point numbers are represented by using this data type. The following example uses the decimal data type for storing totalWeight:
{"totalWeight": 123.456}
- String: This type represents a sequence of zero or more characters. Strings are surrounded with double quotation marks and support a backslash escaping syntax. Here is an example for the string data type:
{"firstName": "Jobinesh"}
- Boolean: This type represents either a true or a false value. The Boolean type is used for representing whether a condition is true or false, or to represent two states of a variable (true or false) in the code. Here is an example representing a Boolean value:
{"isValidEntry": true}
- Array: This type represents an ordered list of zero or more values, each of which can be of any type. In this representation, comma-separated values are enclosed in square brackets. The following example represents an array of fruits:
{"fruits": ["apple", "banana", "orange"]}
- Object: This type is an unordered collection of comma-separated attribute-value pairs enclosed in curly braces. All attributes must be strings and should be distinct from each other within that object. The following example illustrates an object representation in JSON:
{"departmentId":10,
"departmentName":"IT",
"manager":"John Chen"}
- null: This type indicates an empty value, represented by using the word null. The following example uses null as the value for the error attribute of an object:
{"error":null}
The example in the next section illustrates how you can use the JSON data types that we discussed in this section to represent the details of employees.