JSON Data Types
In JSON, values must be one of the following data types:
String: A string is a sequence of characters, surrounded by quotation marks. Strings can contain letters, numbers, and special characters.
{
"text": "Hello World!",
}
//
{
"name": "Marry Doe",
"city": "New York"
}
//
{
"name": "John Smith",
"address": {
"street": "123 Main St",
"city": "Chicago",
"state": "IL"
}
}
Number: A number is a numeric value that can be an integer or a floating-point value.
{
"float": 3.14,
"integer": 100000
}
//array of integer
{
"numbers": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
}
//array of float
{
"prices": [1.124, 3.33, 124.56, 6.80]
}
Boolean: A boolean value can be either true or false.
{
"booleanField": true
}
//array of boolean
{
"answers": [true, false, true, false]
}
Object: An object is a collection of key-value pairs. The keys are strings, and the values can be any valid JSON data type, including another object. Objects are surrounded by curly braces ({}).
{
"name": "Muller",
"age": 30,
"address": {
"street": "562 Main Street",
"city": "New York",
"state": "NY"
}
}
Array: An array is an ordered list of values. The values can be any valid JSON data type, including another array. Arrays are surrounded by square brackets ([]).
[
{
"name":"iPhone",
"manufacturer":"Apple"
},
{
"name":"Galaxy",
"manufacturer":"Samsung"
},
{
"name":"Pixel",
"manufacturer":"Google"
}
]
//more data
[
{
"name":"iPhone",
"manufacturer":"Apple",
"releaseYear":2007,
"screenSize":3.5
},
{
"name":"Galaxy",
"manufacturer":"Samsung",
"releaseYear":2009,
"screenSize":4.0
},
{
"name":"Pixel",
"manufacturer":"Google",
"releaseYear":2016,
"screenSize":5.0
}
]
Null: The null value represents an empty value or the absence of a value.
In JSON, null represents an absence of value or a null value. It is used to indicate that a variable has no value or to mark the end of an object or array.
For example, you could use null as the value for a field in a JSON object like this:
{
"field1": "value1",
"field2": null
}
//Alternatively, you could use null to mark the end of an array like this:
[1, 2, 3, null]
These data types can be used to represent a variety of data structures and types of information.