Introduction to JSON: A Complete JSON Tutorial Series for Beginners

Blog Post Image

Introduction:

Hey there, welcome to the exciting world of JSON! Imagine it as a language that bridges the gap between humans and computers, allowing data to flow seamlessly. So, what's the deal with JSON?

Well, JSON stands for JavaScript Object Notation, and it's like a translator that computers totally get. It's designed to be super light and easy to understand, thanks to the genius of Douglas Crockford.

But wait, there's more! JSON's text-based style makes it a breeze for both humans and machines to handle. Even though it sprouted from the world of JavaScript, it's not playing favourites – it gets along splendidly with many programming languages. What makes it truly awesome is its simplicity, its lightweight nature, and the fact that it's not tied down to any particular language.

Imagine JSON as a global citizen of data sharing, travelling effortlessly between computers, programs, and databases. From sending organised info over the internet to making communication between web apps and servers a cakewalk – JSON's got your back!

So let's understand purpose, properties & syntax of JSON,

Understanding JSON's Purpose

Now, let's zoom in on why JSON matters:

Properties of JSON

Let's dive into the essentials:

Syntax of JSON

By now, you should have a basic understanding of JSON. Now, let's explore its fundamental structure.

JSON is primarily built upon two main components: name-value pairs and ordered lists of values.

JSON serves as a universal data structure, supported by most programming languages available today. This universality allows programmers to work with a single data type that can be used across different programming languages.

Here's a bit more about these data types:

With this foundation in mind, let's delve into a simple JSON structure. For instance, let's consider an example where we're representing car details using JSON.

Let’s assume we have a car object with the following basic properties and their attributes:

So, if we want to transfer this data using a JSON file, then the serialization of this data will create a JSON.

JSON will look something like this:

{
  "Make&Model": "Maruti Suzuki Swift",
  "MakeYear": 2017,
  "Color": "Red",
  "Type": "Hatchback"
}

We have seen about the usage of JSON, its basic structure and how data is presented in the JSON format. Now, let’s take a closer look at how different elements are structured in JSON.

What is a JSON Object?

A JSON object is a combination of keys and their corresponding values, without any fixed sequence.

To group these keys and values, we enclose them in curly braces, indicated by the opening and closing “{ }”. So, for instance, in the earlier example where we crafted JSON related to a car, we were essentially forming a JSON car object. Creating a JSON structure comes with certain guidelines, particularly when dealing with key-value pairs.

So, when building a JSON, the initial step involves defining an attribute. For instance, we're creating an “Employee” JSON object. The subsequent task is to list down the characteristics of this object. Let’s imagine our employees possess attributes like “First Name,” “Last Name,” “Employee ID,” and “Designation.” These attributes of the employee correspond to what we refer to as “Keys” in the JSON structure.

Let’s create a JSON object:

{
  "FirstName": "Sam",
  "LastName": "Jackson",
  "employeeID": 5678923,
  "Designation": "Manager"
}

The entirety of content enclosed within the curly braces constitutes what's called the JSON Employee Object.

A fundamental JSON object is essentially composed of what we call Key-Value pairs. In the earlier instance, we employed JSON to portray employee information.

Within this JSON object, we outlined various characteristics of the employee, including “First Name,” “Last Name,” “Employee ID,” and “Designation.” Each of these characteristics, denoted as “keys,” is associated with a specific value within the JSON. For instance, the key "First Name" holds the value "Sam," and similarly, other keys are linked with their own distinct values.

Generic Rules to be followed while creating a JSON:

Now that you're familiar with the basics of JSON and how it's used, let's take the next step and explore more intricate JSON structures in a simple and understandable way.

A small exercise for all of you!

Try by your own to create a sample JSON describing an “Employee” with your own set of Keys and Values.

Now that you're familiar with the basics of JSON and how it's used, let's take the next step and explore more intricate JSON structures in a simple and understandable way.

JSON Arrays

JSON arrays are like lists you see in most programming languages. They're collections of data items, and in JSON, they're organised using square brackets [ ]. Just like when you make a list, you start with an opening bracket [ and end with a closing bracket ].

Inside these brackets, you put the data items you want in the list, and you separate them with commas. This way, the computer knows where each item begins and ends. It's just like when you make a shopping list, you write down each thing you need and separate them with commas.

Now, let's see an example of an array in JSON. Remember the employee details we talked about before? Well, imagine if an employee knows different programming languages. Instead of listing them one by one, we can use an array to neatly show all the languages they know. It's like making a list of their skills. Just think of it as if you were listing your favourite hobbies, but in a computer-friendly way.

Examples of JSON

{
  "FirstName": "Sam",
  "LastName": "Jackson",
  "employeeID": 5678923,
  "Designation": "Manager",
  "LanguageExpertise": ["Java", "C#", "Python"]
}

As we have already discussed, there are also a few rules that need to be followed while including an array in a JSON. Here are the key points to remember about arrays in JSON:

Now, as we have already discussed the basic structure of JSON, let’s start working on a more complex JSON structure.

Earlier in this tutorial, we gave you two Examples of JSON as shown below.

Employee JSON

{
  "FirstName": "Sam",
  "LastName": "Jackson",
  "employeeID": 5678923,
  "Designation": "Manager",
  "LanguageExpertise": ["Java", "C#", "Python"]
}

Car JSON

{
  "Make&Model": "Maruti Suzuki Swift",
  "MakeYear": 2017,
  "Color": "Red",
  "Type": "Hatchback"
}

Now, consider a scenario where there are multiple employees, each of whom owns a car. To effectively organise this information, we need to nest the car details within the employee's JSON data. This involves creating a nested Car JSON object inside the Employee JSON.

To achieve this, we begin by introducing a new key called "car" in the Employee JSON structure.

Something like this:

{
  "FirstName": "Sam",
  "LastName": "Jackson",
  "employeeID": 5678923,
  "Designation": "Manager",
  "LanguageExpertise": ["Java", "C#", "Python"],
  "Car": {}
}

Including Car in Employee JSON

After adding the "car" key to the employee JSON, we proceed to assign the corresponding car details directly to the Car JSON object.

{
  "FirstName": "Sam",
  "LastName": "Jackson",
  "employeeID": 5698523,
  "Designation": "Manager",
  "LanguageExpertise": ["Java", "C#", "Python"],
  "Car": {
    "Make&Model": "Maruti Suzuki Swift",
    "MakeYear": 2017,
    "Color": "Red",
    "Type": "Hatchback"
  }
}

By following this approach, we can create a nested JSON structure. Consider a scenario where there are numerous employees; in such cases, we can construct a JSON capable of holding data for multiple employees.

[
  {
    "FirstName": "Sam",
    "LastName": "Jackson",
    "employeeID": 5698523,
    "Designation": "Manager",
    "LanguageExpertise": ["Java", "C#", "Python"],
    "Car": {
      "Make&Model": "Maruti Suzuki Swift",
      "MakeYear": 2017,
      "Color": "Red",
      "Type": "Hatchback"
    }
  },
  {
    "FirstName": "Tam",
    "LastName": "Richard",
    "employeeID": 896586,
    "Designation": "Senior Manager",
    "LanguageExpertise": ["Ruby", "C#"],
    "Car": {
      "Make&Model": "Hyundai Verna",
      "MakeYear": 2015,
      "Color": "Black",
      "Type": "Sedan"
    }
  }
]

In the provided example, it's evident that we've included information for two employees. However, when crafting intricate JSON structures like this, there are important points to keep in mind. Always enclose the entire JSON structure within square brackets "[ ]". Additionally, use commas to separate distinct data sets within the JSON, whether they are key-value pairs or JSON objects.

Recommended Practices for Working with JSON:

How to Write appspec.json File:

Assuming you have an application that you want to deploy using AWS CodeDeploy, you'll need to create an appspec.json file to define how the deployment should be carried out. Here's how you can create the appspec.json file step by step:

Version Definition:

{
  "version": 1,
  ...
}

Resources:

{
  "version": 1,
  "Resources": [
    ...
  ],
  ...
}

In the blog, we learned about JSON objects and their key-value pairs. In this context, the "Resources" key is an array that holds objects representing the resources used in the deployment.

Target Service and Type:

{
  "version": 1,
  "Resources": [
    {
      "TargetService": "ecs",
      "Type": "AWS::ECS::TaskDefinition",
      ...
    }
  ],
  ...
}

Here, "TargetService" specifies that the deployment target is an Amazon ECS service, and "Type" specifies that the resource type is an ECS Task Definition.

Properties:

{
  "version": 1,
  "Resources": [
    {
      "TargetService": "ecs",
      "Type": "AWS::ECS::TaskDefinition",
      "Properties": {
        "AppSpecTemplateArtifact": {
          "artifactLocation": "s3://my-codedeploy-bucket/my-appspec.zip",
          "sha256": "hash-of-the-zip-file"
        },
        "TaskDefinitionTemplateArtifact": {
          "artifactLocation": "s3://my-codedeploy-bucket/my-taskdef.zip",
          "sha256": "hash-of-the-zip-file"
        }
      }
    }
  ],
  ...
}

Here, "AppSpecTemplateArtifact" and "TaskDefinitionTemplateArtifact" are properties that define the artifacts (ZIP files) for the AppSpec template and Task Definition template. The "artifactLocation" key specifies the S3 location of the ZIP files, and the "sha256" key provides the hash of the ZIP files.

Hooks:

{
  "version": 1,
  "Resources": [
    {
      "TargetService": "ecs",
      "Type": "AWS::ECS::TaskDefinition",
      "Properties": {
        "AppSpecTemplateArtifact": {
          "artifactLocation": "s3://my-codedeploy-bucket/my-appspec.zip",
          "sha256": "hash-of-the-zip-file"
        },
        "TaskDefinitionTemplateArtifact": {
          "artifactLocation": "s3://my-codedeploy-bucket/my-taskdef.zip",
          "sha256": "hash-of-the-zip-file"
        }
      }
    }
  ],
  "Hooks": [
    {
      "BeforeAllowTraffic": "LambdaFunctionToRunBeforeTrafficShift"
    },
    {
      "AfterAllowTraffic": "LambdaFunctionToRunAfterTrafficShift"
    }
  ]
}

The "Hooks" array defines hooks that run custom Lambda functions before and after traffic shifting during deployment.

Putting it all together, the appspec.json file defines the deployment process for an ECS Task Definition. It specifies the artifact locations, hashes, and hooks necessary to manage the deployment lifecycle.

Remember to replace the placeholder values (s3://my-codedeploy-bucket, hash-of-the-zip-file, LambdaFunctionToRunBeforeTrafficShift, LambdaFunctionToRunAfterTrafficShift) with your actual values based on your application's deployment setup.

Conclusion:

JSON stands as one of the most widely used data formats for transitioning information. Its primary purpose is to facilitate data exchange across various networks. Its text-based format makes it accessible, allowing both users and machines to easily interpret and dissect the data.

While often linked to JavaScript, JSON can be utilised by various programming languages, making it versatile. JSON files carry the .json extension and can be generated using any programming language.

You can craft a basic JSON structure by directly matching key-value pairs or using arrays to assign multiple values to a key. Moreover, JSON can incorporate nested structures, enabling the inclusion of another JSON object within it as a key. This feature enables the transmission of more intricate data using the format.

Share this post:

Back