JavaScript

Valid Data Types in JSON

JSON is the most widely used syntax\notation for transferring data over the internet and internally between various applications. JSON stands for JavaScript Object Notation. JSON is originally mapped to the Object definition of JavaScript, but it is not bound only to JavaScript. The main cause of the popularity of JSON is readability and light-weightedness.

Data is represented in JSON using “key-value” pairs, the first field of this pair is the key that is used to get the value from the JSON object

var JSON = {

"name": "John DOe",

"age": 25,

};

JSON supports 6 different data types which we can use when we want in the value field of the key-value pairs of the JavaScript. These 6 different data types are:

  • Integer
  • String
  • Boolean
  • Null
  • Array
  • object

In this post, we are going to display the use of all 6 data types with their example

Int Datatype

We can use the integer data type in the “value” part of the key-value pairs of Javascript, integer data type in JSON is used without putting any quotation marks as shown:

{
  "Marks": 25,
  "totalScore": 131,
  "Age": 25,
}

String Data Type

To use a string data type we need to wrap it inside double quotation marks, we can use multiple escape sequences as well within the string:

{
  "City" : "New York",
  "Fruit" : "Apple"
}

{
  "Color": "orange",
  "model": "2016Ac2"
}

Null Datatype

If you want to give a value of null to a key-value pair, then you just write the keyword “null”, this would notify the compiler that reads this JSON that this is a nullable object:

{
  "Parent": null,
  "Input": null
}

Boolean Datatype

Boolean data types only contain two values, true or false; To use boolean data type in JSON key-value pairs you simply use the keywords “true” and “false” in lowercase and with no quotation marks:

{
  "Alive": false,
}


{
  "isDigit": false
}

Object Datatype

JSON can be used to transfer objects as well, However, to use objects in your key-value pairs, you need to wrap the object in curly brackets “{ }”. An example of the Object Data Type in the JSON format would be:

{
"person": {
    name = "John Doe",
    Age = 25,
    Married: false,
    Job: "Auditor"
  },
}

Array Datatype

We can even use JSON to transfer arrays. To use arrays in the key-value pairs, we encapsulate the array using the square brackets “[ ] ” just like:

{
  "person1": ["John Doe",25,"Married","Auditor"]
}

We can even use nested arrays in key-value pairs like:

{
  "entry1" : [
    {"id": "007"},
    {"id": "008"},
    {"id" : "009"},
  ]
}

These are all the 6 data types that are supported by JSON for transferring data. Remember, JSON is mapped on the javascript’s object and Javascript object supports the following data types as well:

  • A function
  • Date
  • Undefined

These data types are not supported by the JSON format

Conclusion

JSON supports 6 different data types that we can use in the “key-value” pairs to transfer them over the internet or across applications. JSON transfers data as “key-value” pairs where the first part, the “key” works like the index for reference, and the “Value” is the actual data that we want to transfer. In this post, we learned what those six data types are, how to write them in the value part of the “key-value” pairs of the JSON format.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.