JSON Data Types
Now, we are going to have a look at some of the different datatypes, that can be used as the values for the keys. We are now quite familiar with the thing, that there is going to be some key and some value associated with the key. Also, if we have multiple key-value pairs, we separate them with commas.
The thing is that the value can be of some valid datatype, and now, we are going to study some different valid datatypes that we can use here.
- String
- number
- array
- object
- boolean
- null
Now, since we have a list of some of the types of data, which we can use as values, lets have a look at them one by one.
String :
The string is a Unicode sequence, which is written in double quotes. We can also say that it is a set of characters. So, whenever we need to deal with some data like some name, email, or some kind of text data, we can use the strings there. The important thing to note here is that the strings are supposed to be always in double-quotes.
If you are still confused about this data type, do not worry, since we are going to have a look at an example soon, and things should get clear.
Example for value of string type –
{
“name”:”GyaniPandit”,
“State”:”Maharashtra”
}
In the above example, you can see that the values associated with these different keys are strings. Note again that the strings are supposed to be written in double quotes. So, whenever we need to have such kind of Unicode sequence in our program, we can make use of the strings.
Number :
Well, this type of data requires no different introduction, since its name describes itself. The JSON numbers follow a double-precision floating point format. So, at times, we would be dealing with the numbers in our JSON files. Let’s have a look at some examples, through which we can understand, that how can we use the number of data values.
{
“age”:25,
“marks”:495
}
As you can see, in the above example, the values for the keys – age and marks are the numbers. So, we can use the numbers in our JSON file as values, as and when required. Note that we can make use of integers, and floating-point numbers here.
Object literals :
The JSON object literals are a collection of key-value pairs. So, within our object literal here, we are going to have some key-value pairs, with which we are already familiar. The thing is that we have to follow some rules here, like –
- JSON object literals are surrounded by curly braces.
- They contain key-value pairs.
- If you have multiple key-value pairs, they are separated by commas.
- The values associated with the respective keys must be some valid datatype.
Let’s have a look at some examples, through which, we would be able to understand the object literals in JSON.
{
“Address”:{
“home”:”abc”,
“City”:”Mumbai”,
“State”:”Maharashtra”
}
}
Note that the object literals are surrounded by curly braces. Also, at the top level as well, we are having an object.
So, in this way, whenever we are required to make use of the object literals, we can use them. Also, the thing is that the JSON object literals can be readily used in JavaScript programs, as JavaScript objects, because JSON is a subset of JavaScript.