Friday, 3 January 2020

JSON Syntax | JSON Tutorial

🚀 Master programming and web development step by step!

Subscribe to Ram N Java for beginner-friendly coding tutorials and practical guides!

🔔 Subscribe to Ram N Java

What Are JSON Syntax Rules?

JSON (JavaScript Object Notation) is renowned for being simple, human-readable, and lightweight. However, because computers parse JSON strictly, you must follow specific syntax rules to prevent syntax errors in your web applications.

The foundation of JSON syntax comes directly from JavaScript object literal notation, but with much stricter formatting requirements.

The Core Rules of JSON Syntax

  • Key-Value Pairs: Data is always written in "key": value pairs.
  • Double Quotes Only: In JSON, keys must be wrapped in double quotes. Single quotes ('key') are invalid.
  • Separated by Commas: Multiple key-value pairs or array elements must be separated by commas.
  • Objects Use Curly Braces: JSON objects are enclosed within { }.
  • Arrays Use Square Brackets: Lists or collections are enclosed within [ ].
  • No Trailing Commas: Adding a comma after the last item is not allowed.

Valid Data Types Allowed in JSON

JSON supports the following data types:

  • Strings: Must be enclosed in double quotes (e.g., "Ram").
  • Numbers: Integers or floating-point values without quotes (e.g., 42, 19.99).
  • Booleans: Literal lowercase true or false.
  • Null: Literal lowercase null.
  • Objects: Nested key-value structures inside curly braces.
  • Arrays: Ordered lists enclosed in brackets.

JSON Syntax Example

Here is an example demonstrating correct JSON structure:

{
  "courseName": "Full Stack Java",
  "modulesCount": 12,
  "isAvailable": true,
  "mentor": null,
  "topics": ["Core Java", "Spring Boot", "REST APIs"],
  "details": {
    "durationWeeks": 8,
    "level": "Beginner"
  }
}
  

Common JSON Syntax Mistakes to Avoid

  • Using single quotes instead of double quotes for keys or strings.
  • Placing a trailing comma after the last property or list item.
  • Trying to store functions, undefined values, or comments (JSON does not support comments).

Recommended Tutorials from Ram N Java

Deepen your JSON knowledge with these tutorials:

No comments:

Post a Comment

Tutorials