Skip to content

Structures of JSON

  • by

The basic Structures of JSON object is that contains data in the form of key/value pair. The keys are strings and the values are the JSON types. Keys and values are separated by colon. Each entry (key/value pair) is separated by comma.

basic JSON Structures

Here are the main structures of JSON:

Objects: Objects are enclosed in curly braces {} and contain key-value pairs separated by a colon :. Keys must be strings enclosed in double quotes, while values can be any valid JSON data type, including objects, arrays, strings, numbers, booleans, and null.

{
  "name": "John",
  "age": 30,
  "address": {
    "street": "123 Main St",
    "city": "Anytown",
    "state": "CA",
    "zip": "12345"
  }
}

Arrays: Arrays are enclosed in square brackets [] and contain a comma-separated list of values. The values can be any valid JSON data type, including objects, arrays, strings, numbers, booleans, and null.

[
  {
    "name": "John",
    "age": 30,
    "address": {
      "street": "123 Main St",
      "city": "Anytown",
      "state": "CA",
      "zip": "12345"
    }
  },
  {
    "name": "Jane",
    "age": 25,
    "address": {
      "street": "456 Oak St",
      "city": "Anytown",
      "state": "CA",
      "zip": "12345"
    }
  }
]

Strings: Strings are enclosed in double quotes "" and can contain any Unicode character, including escape characters.

{
  "name": "John Doe",
  "email": "[email protected]",
  "phone": "+1 (555) 555-1212"
}

Numbers: Numbers can be represented as integers, floats, or scientific notation, but they cannot be hexadecimal or octal numbers.

{
  "price": 9.99,
  "quantity": 10,
  "rating": 4.5e-3
}

Booleans: Booleans can be represented as true or false.

{
  "isStudent": true,
  "hasChildren": false
}

Null: Null is represented as the keyword null.

{
“name”: null,
“age”: null,
“address”: null
}

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading