JSON Formatting

JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. It is primarily used to represent object instances using their attributes in a structured manner.

Structure of JSON

Objects

In JSON, objects are defined using curly braces {}. An object is a collection of attribute-value pairs, where each attribute is a string, and the value can be a string, number, object, array, true, false, or null. Each attribute-value pair is separated by a comma.

  • Attribute Names and String Values: Both attribute names and string values are enclosed in double quotes ".
  • Numerical Values: Numerical values are not enclosed in quotes.
  • Attribute-Value Separation: A colon : is used to separate attribute names from their values.

Example

{
  "name": "John Doe",
  "age": 30,
  "isStudent": false
}

Nested Objects

JSON allows for the representation of nested objects. This means an attribute's value can be another JSON object.

Example

{
  "name": "John Doe",
  "address": {
    "street": "123 Main St",
    "city": "Anytown"
  }
}

Arrays

JSON can also represent collections of objects or values using arrays. Arrays are defined using square brackets [], and can contain any combination of JSON objects, strings, numbers, or other arrays.

Example

{
  "name": "John Doe",
  "hobbies": ["reading", "traveling", "swimming"]
}

In this example, the hobbies attribute is an array containing three string values.

Conclusion

JSON formatting provides a simple and flexible way to represent complex data structures. Its use of a clear and concise syntax makes it a popular choice for data interchange between systems, especially in web applications.