JSON | Data Types

19 May, 2021
JSON (JavaScript Object Notation) is most widely used data format for data interchange on the web. JSON is a lightweight text based, data-interchange format and it completely language independent. It is based on a subset of the JavaScript programming language and it is easy to understand and generate. JSON supports mainly 6 data types:  
stringnumberbooleannullobjectarray
string
number
boolean
null
object
array
Note: string, number, boolean, null are simple data types or primitives data types whereas object and array are referred as complex data types. 
 
String: JSON strings must be written in double quotes like C-language there are various special characters(Escape Characters) in JSON that you can use in strings such as \ (backslash), / (forward slash), b (backspace), n (new line), r (carriage return), t (horizontal tab) etc. Example:  
String: JSON strings must be written in double quotes like C-language there are various special characters(Escape Characters) in JSON that you can use in strings such as \ (backslash), / (forward slash), b (backspace), n (new line), r (carriage return), t (horizontal tab) etc. Example:  
{ "name":"Vivek" }

{ "city":"Delhi\/India" }

here \/ is used for Escape Character / (forward slash).
Number: Represented in base 10 and octal and hexadecimal formats are not used. Example:  
Number: Represented in base 10 and octal and hexadecimal formats are not used. Example:  
{ "age": 20 }
{ "percentage": 82.44}
Boolean: This data type can be either true or false. Example:  
Boolean: This data type can be either true or false. Example:  
{ "result" : true }
Null: It is just a define nullable value. Example:  
Null: It is just a define nullable value. Example:  
{
  "result" : true,
  "grade" : null,
  "rollno" : 210
}
Object: It is a set of name or value pairs inserted between {} (curly braces). The keys must be strings and should be unique and multiple key and value pairs are separated by a, (comma). Syntax:  
Object: It is a set of name or value pairs inserted between {} (curly braces). The keys must be strings and should be unique and multiple key and value pairs are separated by a, (comma). Syntax:  
{ key : value, .......}
Example:  
Example:  
{
"Geek":{ "name":"Peter", "age":20, "score": 50.05}
}
Array: It is an ordered collection of values and begins with [ (left bracket) and ends with ] (right bracket). The values of array are separated by ,(comma). Syntax:  
Array: It is an ordered collection of values and begins with [ (left bracket) and ends with ] (right bracket). The values of array are separated by ,(comma). Syntax:  
[ value, .......]
Example:  
Example:  
{
"geek":[ "Sahil", "Vivek", "Rahul" ]
}

{
  "collection" : [
        {"id" : 101},
        {"id" : 102},
        {"id" : 103}
  ]
}
Example of JSON document:  
{
  "Geeks" : [
    { 
      "Geekname" : "Sahil kumar",
      "subject" : "Data structures",
      "Articles" : 10
      },
    { 
      "Geekname" : "Pawan singh",
      "subject" : "Algorithms",
      "Articles" : 16 
      },
    { 
      "Geekname" : "Ayush Goel",
      "subject" : "Networking",
      "Articles" : 7
      }
  ]
}
 
nhyatt0313
gabaa406
JSON
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Remove elements from a JavaScript Array
Roadmap to Learn JavaScript For Beginners
Difference Between PUT and PATCH Request
JavaScript | Promises
How to filter object array based on attributes?
Lodash _.debounce() Method
JavaScript String includes() Method
How to map, reduce and filter a Set element using JavaScript ?
JavaScript | fetch() Method