|

json data types with examples

json data types with example

JSON (JavaScript Object Notation) supports six basic data types. They are divided into two categories: primitive (simple values) and complex (structures that can hold multiple values).

Here is a breakdown of each JSON data type with examples.

Primitive Data Types

1. String

A string is a sequence of characters used to represent text. In JSON, strings must always be enclosed in double quotes ("").

JSON

{
  "name": "Jane Doe",
  "city": "Hyderabad",
  "greeting": "Hello, world!"
}

2. Number

Numbers in JSON can be integers (whole numbers) or floating-point numbers (decimals). They can also be negative. Unlike strings, numbers are never enclosed in quotes.

JSON

{
  "age": 28,
  "temperature": 98.6,
  "balance": -150.50
}

3. Boolean

A boolean represents a logical entity and can only have one of two values: true or false. These are reserved keywords and should not be enclosed in quotes.

JSON

{
  "isSubscribed": true,
  "hasErrors": false
}

4. Null

The null value is used to represent the intentional absence of any object value. It means “empty” or “nothing.” Like booleans, it is written without quotes.

JSON

{
  "middleName": null,
  "deletedAt": null
}

Complex Data Types

5. Object

An object is an unordered collection of key-value pairs enclosed in curly braces {}.

  • The keys must always be strings (in double quotes).
  • The values can be any valid JSON data type (including another object).

JSON

{
  "employee": {
    "id": 101,
    "department": "Engineering",
    "isActive": true
  }
}

6. Array

An array is an ordered list of values enclosed in square brackets []. The values inside an array can be of any valid JSON data type, and you can mix different types within the same array (though it is best practice to keep them uniform).

JSON

{
  "tags": ["web", "development", "json"],
  "scores": [95, 88, 100],
  "mixedArray": ["apple", 42, true, null]
}

Complete Example

Here is how all six data types look when combined into a single, structured JSON payload:

JSON

{
  "username": "coder_jane",
  "accountAgeDays": 342,
  "isPremium": true,
  "subscriptionEndDate": null,
  "skills": ["JavaScript", "Python", "SQL"],
  "profile": {
    "theme": "dark",
    "notificationsEnabled": false
  }
}

In JSON, there is technically only one Array data type, which is an ordered list of values enclosed in square brackets [].

However, because an array can contain any valid JSON data type, we can categorize them based on what they hold. Arrays can hold a single type of data (homogeneous) or a mix of different types (heterogeneous).

Here are the different ways arrays are commonly structured in JSON, complete with examples.

1. Array of Strings

This is a simple list of text values. It is commonly used for things like tags, categories, or names.

JSON

{
  "fruits": ["apple", "banana", "mango", "orange"]
}

2. Array of Numbers

An array containing only numerical values (integers or floating-point numbers). Often used for scores, coordinates, or data points.

JSON

{
  "testScores": [95, 82, 100, 74.5, 88]
}

3. Array of Booleans

An array containing only true or false values.

JSON

{
  "featureToggles": [true, false, true, true]
}

4. Array of Objects

This is one of the most common array structures used in web APIs. It holds a list of JSON objects, which is perfect for representing multiple records, like a list of users or products.

JSON

{
  "employees": [
    {
      "id": 1,
      "name": "Alice",
      "role": "Developer"
    },
    {
      "id": 2,
      "name": "Bob",
      "role": "Designer"
    },
    {
      "id": 3,
      "name": "Charlie",
      "role": "Manager"
    }
  ]
}

5. Multi-dimensional Arrays (Array of Arrays)

An array can contain other arrays. This is useful for representing matrices, grids, or grouped data.

JSON

{
  "ticTacToeBoard": [
    ["X", "O", "X"],
    ["O", "X", "O"],
    ["O", "X", "X"]
  ]
}

6. Mixed (Heterogeneous) Arrays

JSON does not enforce a single data type across an array. You can mix strings, numbers, booleans, objects, arrays, and null within the exact same list. (Note: While valid, mixing types is generally discouraged in API design because it makes parsing more difficult).

JSON

{
  "mixedData": [
    "Hello",          
    42,               
    true,             
    null,             
    {"key": "value"}, 
    [1, 2, 3]         
  ]
}

In JSON, an Object is an unordered collection of key-value pairs, enclosed within curly braces {}.

There are two strict rules for formatting an object:

  1. Keys must always be strings enclosed in double quotes (e.g., "name").
  2. Values can be any valid JSON data type: string, number, boolean, null, array, or even another object.

Because an object’s values can be anything, you can structure them in various ways depending on the complexity of your data. Here are examples of different ways objects are used.

1. Simple (Flat) Objects

A simple object contains only primitive data types (strings, numbers, booleans, and null) as values. There is no deep nesting.

JSON

{
  "make": "Toyota",
  "model": "Corolla",
  "year": 2024,
  "isElectric": false,
  "previousOwner": null
}

2. Nested Objects (Objects within Objects)

An object can contain another object as a value. This is highly useful for grouping related information together hierarchically, such as an address belonging to a user.

JSON

{
  "userId": 8472,
  "username": "hyderabad_dev",
  "contactInfo": {
    "email": "dev@example.com",
    "phone": "555-0198",
    "address": {
      "city": "Hyderabad",
      "postalCode": "500001",
      "country": "India"
    }
  }
}

3. Objects with Arrays

Objects frequently contain arrays to represent a list of items belonging to that specific object. For example, a restaurant object might contain a list of menu items or operating hours.

JSON

{
  "restaurantName": "Spice Garden",
  "isOpen": true,
  "popularDishes": ["Biryani", "Butter Chicken", "Naan"],
  "ratings": [5, 4, 5, 4, 3]
}

4. Complex (Composite) Objects

In real-world applications (like API responses), JSON objects often combine all data types, including deeply nested objects and arrays of objects, to represent complex data structures.

JSON

{
  "orderId": "ORD-99321",
  "customer": {
    "name": "Jane Doe",
    "isPremiumMember": true
  },
  "items": [
    {
      "productId": "A100",
      "name": "Wireless Mouse",
      "quantity": 1,
      "price": 29.99
    },
    {
      "productId": "B200",
      "name": "Mechanical Keyboard",
      "quantity": 2,
      "price": 89.50
    }
  ],
  "shippingDetails": {
    "carrier": "FedEx",
    "trackingNumber": null
  },
  "totalAmount": 208.99
}

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *