Web Designing 2 — Study Notes

Unit 03

JSON — JavaScript Object Notation

4 Questions 6 Marks Each Web Designing 2
01
6 Marks Question

Concept and Features of JSON

Concept of JSON

JSON (JavaScript Object Notation) is a lightweight, human-readable data interchange format used to store and transfer data between systems. It is widely used in web applications, especially in APIs.

JSON is language-independent, meaning data can be created in one programming language and read in another.

Basic Characteristics of JSON

Structure Rules
  • Data is stored in name/value pairs
  • Objects are enclosed in curly braces { }
  • Arrays are enclosed in square brackets [ ]
  • Values are separated by commas
Example
{
  "name": "BCA",
  "age": 30,
  "car": null
}

Features of JSON

Lightweight and Text-Based
JSON is simple text, so it is easy to transmit over networks.
Human Readable
Uses clear key–value pairs, making data easy to understand.
Language Independent
Although derived from JavaScript, JSON is supported by many languages like Java, Python, and PHP.
Easy to Parse and Generate
JSON can be quickly converted to objects using methods like JSON.parse() and JSON.stringify().
Supports Multiple Data Types
JSON supports string, number, boolean, null, object, and array.
Widely Used in Web APIs
Commonly used in REST APIs, configuration files, and client-server communication.
Structured Data Format
Represents complex data using nested objects and arrays.
Conclusion JSON is a simple, lightweight, and widely supported data format used for efficient data storage and exchange between client and server.
02
6 Marks Question

Similarity and Difference Between XML and JSON

XML (eXtensible Markup Language) and JSON (JavaScript Object Notation) are both widely used data interchange formats for storing and transferring structured data between systems.

Similarities Between XML and JSON

Used for Data Exchange
Both XML and JSON are used to store and transfer structured data between client and server.
Human Readable
Both formats are text-based and easy for humans to read and understand.
Language Independent
Data can be created in one programming language and used in another.
Hierarchical Structure
Both support nested data structures.
Widely Used in Web Applications
Commonly used in APIs, configuration files, and web services.

Differences Between XML and JSON

BasisXMLJSON
Full FormExtensible Markup LanguageJavaScript Object Notation
ComplexityMore complex to learnEasy to learn
Data FormatUses tagsUses key–value pairs
Read/WriteMore verboseSimple and compact
SecurityMore secureLess secure
Display CapabilityCan display data (markup)No display capability
Array SupportDoes not support arrays directlySupports arrays
Based onSGMLJavaScript
Closing TagsRequiredNot required

Example Comparison

XML
<student>
  <name>ABC</name>
  <age>21</age>
  <course>BCA</course>
</student>
JSON
{
  "name": "ABC",
  "age": 21,
  "course": "BCA"
}
Conclusion Both XML and JSON are used for data interchange, but JSON is lighter and easier to use, while XML is more structured and feature-rich.
03
6 Marks Question

JSON Objects and Arrays

A JSON object is a collection of key–value pairs enclosed in curly braces { }. Keys are always strings in double quotes; values can be string, number, boolean, array, object, or null.

Syntax
{
  "key": value
}
Example — String values
{
  "name": "ABC",
  "course": "BCA",
  "city": "Bharuch"
}

"name", "course", "city" are keys; "ABC", "BCA", "Bharuch" are string values.

Example — Number values
{
  "rollNo": 25,
  "age": 20,
  "fees": 15000.50
}

Numbers in JSON are written without quotes.

A JSON array is an ordered list of values enclosed in square brackets [ ]. It can contain strings, numbers, booleans, objects, or other arrays.

Syntax
[
  value1,
  value2,
  value3
]
Example — Array of strings
{
  "subjects": [
    "Java",
    "DBMS",
    "Web Technology"
  ]
}
Example — Array of objects
{
  "employees": [
    {
      "name": "Sonoo",
      "email": "sonoo@gmail.com"
    },
    {
      "name": "Rahul",
      "email": "rahul@gmail.com"
    }
  ]
}

This shows how complex data can be represented using an array of objects.

Conclusion JSON objects store data as key–value pairs, while JSON arrays store ordered lists of values. Together, they help represent simple to complex structured data efficiently.
04
6 Marks Question

Comments in JSON

Definition

JSON does not officially support comments. The JSON standard is designed to be lightweight and strict, so comment syntax like // or /* */ is not allowed in pure JSON.

Explanation

  • JSON is meant for data interchange only.
  • To keep parsing simple and fast, comments are excluded.
  • If comments are added, many JSON parsers will throw an error.
Invalid JSON — Comments Not Allowed
{
  // This is not allowed
  "name": "ABC"
}
Adding // comments will cause a parser error.
Workaround — Common Practice
{
  "_comment": "Student information",
  "name": "ABC"
}
This is valid because _comment is treated as a normal data field. Developers use dummy fields like this to add notes.
Conclusion Standard JSON does not support comments. Any explanation must be added as a normal data field if needed.