JSON Data Example | File Example


JSON data can be stored in a file or can be sent over the network. It can range from simple key-value pairs to complex nested objects.

In this tutorial, you will look at various different types of JSON data starting with very simple example to complex nested data structures.


JSON Data Example

JSON output from different APIs is different, so in the process of learning JSON you should have a look at different JSON data examples.

The following example reprensents different formats a JSON data can take.

Example 1:

The following example shows a simple JSON data with key-value pairs representing a student.

{
  "name": "Alice",
  "major": "Computer Science",
  "age": 21
}

The above JSON object represents a student with a name, major, and age. The name is a string, the major is a string, and the age is a number.


Example 2:

The following example represents a simple array. An array is a list of values, where each value is separated by a comma.

[50, 100, 150, 200]

Example 3:

The following example represents an array with multiple data types.

[50, "Hello", true, null]

Example 4:

The following example is array of objects, each representing multiple students.

[
  {
    "name": "Alice",
    "major": "Computer Science",
    "age": 21
  },
  {
    "name": "Bob",
    "major": "Mechanical Engineering",
    "age": 22
  }
]

The above example represents an array 2 student objects.


Example 5:

This is an object containing an array of students in a specific course:

{
  "course_name": "Intro to Data Structures",
  "students": [
    {
      "name": "Alice",
      "major": "Computer Science"
    },
    {
      "name": "Bob",
      "major": "Computer Science"
    },
    {
      "name": "Eve",
      "major": "Computer Science"
    }
  ]
}

Above JSON object represents a course with a name and a list of students. The course name is a string and the students are represented as an array of objects, each with a name and a major.


Example 6:

This is an example of an object with multiple nested layers:

{
  "name": "John Smith",
  "age": 30,
  "personal_info": {
    "address": {
      "street": "123 Main St",
      "city": "New York",
      "state": "NY"
    },
    "phone_numbers": [
      "555-555-1212",
      "555-555-1213"
    ]
  }
}

The above JSON object represents a person with a name, age, and personal information. The personal information is an object with an address and a list of phone numbers.


Example 7:

This is an example an object with a nested array of objects:

{
  "invoices": [
    {
      "id": 1,
      "total": 100.00,
      "items": [
        {
          "name": "item 1",
          "quantity": 2,
          "price": 50.00
        },
        {
          "name": "item 2",
          "quantity": 1,
          "price": 50.00
        }
      ]
    },
    {
      "id": 2,
      "total": 200.00,
      "items": [
        {
          "name": "item 3",
          "quantity": 1,
          "price": 200.00
        }
      ]
    }
  ]
}

The above JSON object represents a list of invoices. Each invoice has an id, total, and a list of items paid for. Each item has a name, quantity, and price.


Example 8:

This is an example of an object with a nested array of objects with a nested array of objects:

{
  "movies": [
    {
      "title": "The Matrix",
      "year": 1999,
      "cast": [
        {
          "name": "Keanu Reeves",
          "role": "Neo"
        },
        {
          "name": "Laurence Fishburne",
          "role": "Morpheus"
        },
        {
          "name": "Carrie-Anne Moss",
          "role": "Trinity"
        }
      ]
    },
    {
      "title": "Lord of the Rings",
      "year": 2001,
      "cast": [
        {
          "name": "Elijah Wood",
          "role": "Frodo"
        },
        {
          "name": "Ian McKellen",
          "role": "Gandalf"
        },
        {
          "name": "Viggo Mortensen",
          "role": "Aragorn"
        }
      ]
    }
  ]
}

The above JSON object represents a list of movies. Each movie has a title, year, and a list of cast members. Each cast member has a name and a role.

You can see the above JSON is formatted in a way that is easy to read. This is because it is formatted using JSON Formatter.

Conclusion

Looking at the various different types of JSON examples now you have a better understanding of how JSON data is structured.

You may get much more complex JSON data in real-world applications. But, the basic structure of JSON data is always the same. It is an array or an object.

See how to work with JSON in JavaScript.

Happy Learning!