JSON Basics - a beginner's guide for API Testing

JSON Basics - a beginner's guide for API Testing

learn to read JSON responses to be able to create test cases in Postman

1 Who this is for

  • if you are looking to learn about JSON data format

  • if you already got the postman installed as per these instructions and now looking for the next step

  • if you are learning API testing using postman

2 Learning Objectives

  • understand datatype like Numbers, Strings, boolean

  • to read simple Objects

  • to read simple Arrays

  • to read nested Objects

  • to read complex Arrays

  • practice problems

3 Introduction

In this post, the focus is to capture the absolute basics from a beginner's perspective with the intention of providing precise actionable information without any academic pretense. This is intended for learners who are keen to acquire skills in API Testing and the ability to read and write JSON objects is arguably at the core of being a skilled QA for API Testing.

If you are flabbergasted why all of a sudden we are so heavy on JSON in the context of API testing, keep reading .......

3.1 Understanding the context

  • The ability to comprehend JSON shouldn't be tied to API Testing.

  • It should be treated as an essential skill if you are planning to work with API in any capacity regardless of whether your role is considered technical or non-technical. For eg, Product Teams, Scrum Masters / Delivery Managers / QA Teams

  • This is just a data representation format, one of many but JSON needs dedicated time as it has become a de facto standard for data exchanges for API-based communication

  • HTML is another example of a data representation format that humans usually consume in the form of websites and Apps

  • Unlike HTML, JSON is only meant for software-to-software communication as it has become

  • There are only a few rules/syntax to understand which makes it easy to learn and implement

3.2 Where does knowledge of JSON fit in the path of learning API Testing?

In an over simplified view, API Testing is a 5 steps process:

Step-1: Requirement Analysis (at its core) for testing one endpoint usually involves asking the following questions :

  • a What kind of request is this? GET / POST / PUT / DELETE etc?

  • b What is the request URL?

  • c What is the expected response?

Step-2: As per the requirements, the Request is prepared in a tool like Postman

Step-3: Again, as per requirements, you would be provided with an example of an expected response usually in the form of JSON.

Test cases are then written to validate Actual Results against Expected responses.

Tests are written within Postman (assuming the API testing tool used is Postman)

Step-4: Make the API Call as prepared in Step 2. That will also execute the tests written in Step 3 and mark the test case as PASS / FAIL based on the comparison between Actual results and Expected results

Step-5: Reporting test results

As an API tester, primarily you would be able to apply knowledge of JSON in Step 3 because this is where you need to be able to read the JSON response and create Tests

"Knowledge is NOT power. Knowledge is only POTENTIAL. Understanding Why and Where to apply that knowledge is WISDOM. Applying that knowledge is POWER."

3.3 Is JSON the only thing I should know?

No. There are many other formats however to keep focus we are sticking with only the most common one. Refer to this link if you want to get a bird's eye view of what other formats are typically supported

4 Prerequisites

  • 1. Have already got postman installed. Refer to this link, if the setup is not done

  • 2. Install visual code from here (Optional)

5 JSON: Introduction to Objects

5.1 Numbers, Boolean & String

a. String

  • [tip] Anything written within single/double quotes is a String in the API response

  • Following are a few examples of String

    • 'I am a string'

    • "Another line as string"

    • "Abcd1235"

    • 'Hello'

    • "786017818"

    • "0"

    • ""

    • " "

    • ''

    • ' '

    • "false"

    • "TRUE"

b. Numbers

  • [tip] Any Numeric value without quotes and spaces

  • Following are a few examples of Numbers

    • 0

    • 1

    • 100

    • -1

    • -9827

c. Boolean

  • [tip] values true or false without quotes and spaces are termed as boolean

  • Following are examples of valid Boolean

    • true

    • false

d. Arrays

  • an array is a collection of similar items & is enclosed within square brackets [ ]

  • Since an array is a collection of similar items, we should always clarify the what is the type of items collected in the array. Hence, it is always read as array of

  • Following are examples of a few arrays

    • array of Number => [1, 5, 10, 555, 71, 0]

    • array of Boolean => [true, false, false, true]

    • array of String => ["Sam", "Sean", "Jim", "Oscar", "Denzel"]

    • Empty array => []

5.2 Characteristics of a JSON Object

5.2.1 Nuts & Bolts

  • It is enclosed within curly braces { & }

  • It contains a key and value pair separated by a colon :

  • An object can have any number of key-value pairs. Two key-value pair is separated by a comma ,

  • key in the key-value pair of JSON is also called property. So in the above example, we have two

5.2.2 Examples - Reading JSON objects with explanation

Train your mind to read JSON objects with specific vocabularies, this can help when deciphering complex objects.

  1. Sample Resonse : An Object with employee information

    Explanation:

    • line 1: marks the start of an object with opening curly braces {

    • line 2: is a key-value pair where firstName is the name of the property that contains a value of type String

    • line 3: is a key-value pair where lastName is the name of the property that contains a value of type String

    • line 4: is a key-value pair where age is the name of the property that contains a value of type Number

    • line 5: is a key-value pair where departmentCode is the name of the property that contains a value of type String

    • line 6: is a key-value pair where married is the name of the property that contains a value of type Boolean

    • line 7: marks the end of the object with closing curly braces }

    • This object contains 5 key-value pairs, where 3 properties are of type String, 1 is Number, and 1 is Boolean

  2. Sample Response : Object with statictics for a blog

    Explanation:

    • line 1: is the start of the object

    • line 2: is a key-value pair where category is the name of the property that contains a value of type String

    • line 3: is a key-value pair where id is the name of the property that contains a value of type String

    • line 4: is a key-value pair where name is the name of the property that contains a value of type String

    • line 5: is a key-value pair where description is the name of the property that contains a value of type String

    • line 6: is a key-value pair where published is the name of the property that contains a value of type Number

    • line 7: is a key-value pair where drafts is the name of the property that contains a value of type Number

    • line 8: is a key-value pair where url is the name of the property that contains a value of type String

    • line 9: is a key-value pair where posts is the name of the property that contains a value of type Object

    • line 13: is a key-value pair where author is the name of the property that contains a value of type String

    • line 9 - line 12 : line 9 contains key-value pair, where

    • line 14: is the end of the object

  3. Sample Response : Object with Air Quality Index data for a City

    • The response is an object that contains 4 properties : 3 of type String and 1 is an Array of objects

    • line 1: is the start of the object

    • line 12: is the end of the object

    • line 2: is a key-value pair where citi_name is the name of the property that contains value ( Raleigh ) of type String

    • line 3: is a key-value pair where state_code is the name of the property that contains value ( NC ) of type String

    • line 4: is a key-value pair where data is the name of the property that contains a value of type Array

      • Array of Objects

      • In this array example from line 5 to line 11, there is the only item in the Array, hence the size or length of the Array data = 1

      • Each item is an object that contains there properties:

        • aqi, pm25, pm10 of data type String
  4. Sample Response : Array of objects with comments from users to blogs

  • The API response is an Array of Objects that contains 4 items where each item is an object that contains 5 properties

    • postId is of type Number

    • id is of type Number

    • name is of type String

    • email is of type String

    • body is of type String

  • line 1: is the start of the Array

  • line 30: is the end of the Array

  • The size/Length of the Array is equal to the total number of items, which mean size of the array in this example is 4

  • The first item of the Array is accessed by index 0

  • The last item in this Array can be accessed using index 3 (using the formula size - 1)

6 DIY: Learn by doing to be added

Did you find this article valuable?

Support Himanshu Pandey by becoming a sponsor. Any amount is appreciated!