Quick Bites on JSON for QA Professionals

                                   JSON for Novice QA

What Is JSON?

JSON is short for JavaScript Object Notation, and is a way to store information in an organized, easy-to-access manner. In a nutshell, it gives us a human-readable collection of data that we can access in a really logical manner.
JSON can be thought of as a hierarchical set of key/value pairs where the value can be:
• Object - delimited by { and }.
• Array - delimited by [ and ].
• String - delimited by " and ".
• Integer

Storing JSON Data

As a simple example, information might be written in JSON as follows:

var jason = {
"age" : "24",
"hometown" : "Missoula, MT",
"gender" : "male"
};

This creates an object that we access using the variable jason. By enclosing the variable's value in curly braces, we're indicating that the value is an object. Inside the object, we can declare any number of properties using a "name": "value" pairing, separated by commas. To access the information stored in jason, we can simply refer to the name of the property we need. For instance, to access information about me, we could use the following snippets:

document.write('Jason is ' jason.age); // Output: Jason is 24
document.write('Jason is a ' jason.gender); // Output: Jason is a male

Storing JSON Data in Arrays

A slightly more complicated example involves storing two people in one variable. To do this, we enclose multiple objects in square brackets, which signifies an array. For instance, if I needed to include information about myself and my brother in one variable, I might use the following:

var family = [{
    "name" : "Jason",
    "age" : "24",
    "gender" : "male"
},
{
    "name" : "Kyle",
    "age" : "21",
    "gender" : "male"
}];

To access this information, we need to access the array index of the person we wish to access. For example, we would use the following snippet to access info stored in family:

document.write(family[1].name); // Output: Kyle
document.write(family[0].age); // Output: 24

Nesting JSON Data

Another way to store multiple people in our variable would be to nest objects. To do this, we would create something similar to the following:

var family = {
    "jason" : {
        "name" : "Jason Lengstorf",
        "age" : "24",
        "gender" : "male"
    },
    "kyle" : {
        "name" : "Kyle Lengstorf",
        "age" : "21",
        "gender" : "male"
    }
}

Accessing information in nested objects is a little easier to understand; to access information in the object, we would use the following snippet:

document.write(family.jason.name); // Output: Jason Lengstorf
document.write(family.kyle.age); // Output: 21
document.write(family.jason.gender); // Output: male


An array is a list of objects or key/value pairs.
The keys are String values e.g. “projects”, “project”, “id”, etc.

{
"projects": {
"project": [
{
"id": 1,
"name": "KnowledgeHunt",
"position": 0,
"description": "JSON Decoded for QA",
"state": "active",
"created-at": "2018-02-03T12:25:26+01:00",
"updated-at": "2018-02-04T12:25:26+01:00"
}
]
}
}

Comments

Popular posts from this blog

QA's approach 2 Java - Understanding Static context

Selenium 4 absolute beginners - How to create Batch execution file

Technologies - Log4J - Create Log4j Configuration File - Where ? How ? What ?