JSON: A Beginner's Guide to JavaScript Object Notation

JSON

JSON, which stands for JavaScript Object Notation, is a lightweight data interchange format. It is easy for humans to read and write, and easy for machines to parse and generate. JSON is a text format that is language-independent and uses conventions familiar to programmers of many languages, including C, C++, Java, Kotlin, Python, JavaScript, and others.



JSON data is represented as key-value pairs, similar to how data is stored in a dictionary in Python or an object in JavaScript. The basic structure of JSON includes:

JSON Object:

An unordered collection of key-value pairs enclosed in curly braces {}.
Each key is a string and is followed by a colon, then its corresponding value.
Multiple key-value pairs are separated by commas.


Example:
{
  "name": "Atif Pervaiz",
  "age": 27,
  "city": "Lahore"
}


JSON Array:

An ordered list of values enclosed in square brackets [].
Values in an array can be of any data type, including objects or other arrays.


Example:
[
  "apple",
  "banana",
  "orange"
]


JSON Value:

A JSON value can be a string, number, object, array, boolean (true or false), or null.


JSON is commonly used for data exchange between a server and a web application, and it is a standard data format with diverse applications in various programming languages and technologies. When working with JSON in a programming language, you can typically use libraries or built-in functions to encode (convert data to JSON) and decode (convert JSON to data) seamlessly.

Complex Example

This JSON represents information about a group of users in Pakistan and their associated details. Here's a short description:

info: Contains general information, including the status (OK), the country (Pakistan), and a link to the server.

users: An array containing multiple user objects, each representing an individual user. Each user object includes the following details:
  • user_id: Unique identifier for the user.
  • email: Email address of the user.
  • name: Name of the user.
  • logins_count: Number of logins for the user.
  • created_at: Timestamp indicating when the user account was created.
  • updated_at: Timestamp indicating when the user account was last updated.
  • last_login: Timestamp indicating the user's last login.
  • email_verified: A boolean indicating whether the user's email is verified.
  • profileImage: URL to the user's profile image.
This JSON structure is commonly used to exchange data between a server and a client, often in the context of a web application or a mobile app.

{
  "info": {
    "status": "OK",
    "country": "Pakistan",
    "server": "https://play.google.com/store/apps/dev?id=9049012648188611488"
  },
  "users": [
    {
      "user_id": "0101",
      "email": "test1@test.com",
      "name": "Test 1",
      "logins_count": 15,
      "created_at": "2024-11-28T14:10:11.338Z",
      "updated_at": "2024-12-02T01:17:29.310Z",
      "last_login": "2024-12-02T01:17:29.310Z",
      "email_verified": true,
      "profileImage": "https://blogger.googleusercontent.com/img/a/AVvXsEj29LIlmZ_29SgyUIJv7WGJrtFwkso9uAjBqIWtPSo_jYcklU6DUmtxtG6KFRsAuXINZrCXCMFWBAnLpLxL_K1M6MJn2AUW91-bw30GpJpxs-ZNZC9Wpeh01Oe7W4jG_3taQfmjLFHJIt0d4rSAIOkx0n9vS2fAKRZtb-arthN2mIyhvUAV3FMkagyfZH5u=w800"
    },
    {
      "user_id": "0102",
      "email": "test2@test.com",
      "name": "Test 2",
      "logins_count": 15,
      "created_at": "2024-11-28T14:10:11.338Z",
      "updated_at": "2024-12-02T01:17:29.310Z",
      "last_login": "2024-12-02T01:17:29.310Z",
      "email_verified": true,
      "profileImage": "https://blogger.googleusercontent.com/img/a/AVvXsEj29LIlmZ_29SgyUIJv7WGJrtFwkso9uAjBqIWtPSo_jYcklU6DUmtxtG6KFRsAuXINZrCXCMFWBAnLpLxL_K1M6MJn2AUW91-bw30GpJpxs-ZNZC9Wpeh01Oe7W4jG_3taQfmjLFHJIt0d4rSAIOkx0n9vS2fAKRZtb-arthN2mIyhvUAV3FMkagyfZH5u=w800"
    },
    {
      "user_id": "0103",
      "email": "test3@test.com",
      "name": "Test 3",
      "logins_count": 15,
      "created_at": "2024-11-28T14:10:11.338Z",
      "updated_at": "2024-12-02T01:17:29.310Z",
      "last_login": "2024-12-02T01:17:29.310Z",
      "email_verified": true,
      "profileImage": "https://blogger.googleusercontent.com/img/a/AVvXsEj29LIlmZ_29SgyUIJv7WGJrtFwkso9uAjBqIWtPSo_jYcklU6DUmtxtG6KFRsAuXINZrCXCMFWBAnLpLxL_K1M6MJn2AUW91-bw30GpJpxs-ZNZC9Wpeh01Oe7W4jG_3taQfmjLFHJIt0d4rSAIOkx0n9vS2fAKRZtb-arthN2mIyhvUAV3FMkagyfZH5u=w800"
    },
    {
      "user_id": "0104",
      "email": "test4@test.com",
      "name": "Test 4",
      "logins_count": 15,
      "created_at": "2024-11-28T14:10:11.338Z",
      "updated_at": "2024-12-02T01:17:29.310Z",
      "last_login": "2024-12-02T01:17:29.310Z",
      "email_verified": true,
      "profileImage": "https://blogger.googleusercontent.com/img/a/AVvXsEj29LIlmZ_29SgyUIJv7WGJrtFwkso9uAjBqIWtPSo_jYcklU6DUmtxtG6KFRsAuXINZrCXCMFWBAnLpLxL_K1M6MJn2AUW91-bw30GpJpxs-ZNZC9Wpeh01Oe7W4jG_3taQfmjLFHJIt0d4rSAIOkx0n9vS2fAKRZtb-arthN2mIyhvUAV3FMkagyfZH5u=w800"
    }
  ]
}

Comments

Popular posts from this blog

Manage External Storage Permission | Android Studio | Kotlin

Manage External Storage Permission | Android Studio | Java

Add a Back Button to Action Bar Android Studio (Kotlin)