JSON Data Types

Profile picture for user arilio666

A JSON body can comprise of one of 6 data types which can be categorized as primitive and complex data types.

  1. String (Primitive)
  2. Boolean (Primitive)
  3. Null (Primitive)
  4. Number (Primitive)
  5. Object (Complex)
  6. Array (Complex)

JSON Primitive / Simple Data Types

1. JSON String Datatype

String, as usual, must be written within double quotes like any other. String supports various special character as well including

  •  \\ (backslash)
  • \" (Double Quotes)
  • \/ (forward slash)
  • \b (backspace)
  • \n (new line)
  • \r (carriage return)
  • \t (horizontal tab)
  • \f (form feed)
  • \u (trailed by 4 hex digits)
{"Hero": "Eren"}

2. JSON Boolean Datatype

  • Values can either be true or false in this.
  • Do not surround boolean with double quotes
{"Paid": true}

3. JSON Null

  • It is just a null or empty value. When no value is assigned to key it is treaded as null.
  • null is a special value in JSON
  • null is also not surrounded by double quotes
{"LastName": null}

4. JSON Number

  • Either JSON must use integer or float here.
  • Octal and hexadecimal values are not used.
  • Also Nan and infinity values are not allowed.
  • Include digits 0-9
  • can be a negative number -5
  • can be a fraction value .5
  • can include exponent of 10, prefixed by e or E 
  • Numbers are not surrounded by double quotes
{"Points": 10}

JSON Complex / Compound / Structure Data Types

1. JSON Object

  • JSON values can also be objects with properties of key-value pairs.
  • Objects are enclosed within curly braces. Starts with { and ends with }. 
{"employee":{"Hero":"Naruto", "age":30, "city":"HiddenLeaf"}}

2. JSON Array

  • JSON body can also represent JSON values in the form of an array list.
  • It is an ordered collection of values. Arrays starts with [ and ends with ] bracket. 
{"Stones": ["Time","Mind","Power"]}

JSON value cannot comprise of these:

  1. Function
  2. Date
  3. Undefined

JSON Body Example

{
    "store": {
        "rice": [
            {
                "category": "brown rice",
                "brand": "oro brown rice",
                "price": 8.95
            },
            {
                "category": "red rice",
                "brand": "blured red rice",
                "price": 12.99
            }
        ],
        "milk": {
            "color": "blue",
            "price": 19.95
        }
    },
    "expensive": 30
}

So these are the datatypes and conditions to be followed while creating and giving values to a JSON body structure.