JsonPath and XmlPath in Rest Assured

What we have learned so far?

Part 1 – What is Rest Assured? How to setup Rest Assured?
https://www.onlyfullstack.com/what-is-rest-assured-how-to-setup-rest-assured/

Part 2 – Sample Rest API To Test With Rest Assured
https://www.onlyfullstack.com/sample-rest-api-to-test-with-rest-assured/

Part 3 – How to make a GET Request using Rest Assured – Only Fullstack
https://www.onlyfullstack.com/how-to-send-get-request-in-rest-assured/

Part 4 – How to make a POST Request using Rest Assured – Only Fullstack
https://www.onlyfullstack.com/how-to-send-post-request-in-rest-assured/

Part 5 – How to make a PUT Request using Rest Assured – Only Fullstack
https://www.onlyfullstack.com/how-to-send-put-request-in-rest-assured/

Part 6 – How to make a DELETE Request using Rest Assured – Only Fullstack
https://www.onlyfullstack.com/how-to-send-delete-request-in-rest-assured/

What is JsonPath in Rest Assured?

Rest apis returns the response in json format. we can parse the Json and compare the objects but what if we want to compare only specific property or attribute of the Json?
JsonPath is used to get the value of any particular property or attribute from the json.
So far we have been providing the JsonPath to our body method to validate the response. Lets dive into the JsonPath and understand different scenarios to get the values for a specific property.

We have saved the below Json into json-path-example.json file –

{
  "store": {
    "book": [
      {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
}

Lets find out below properties form above json –

  1. the bicycle information – store.bicycle
  2. First book in the list – store.book[0]
  3. Author of the first book – store.book[0].author
package onlyfullstack.verifyResponse;

import io.restassured.path.json.JsonPath;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class JsonPathExample {

    String exampleJson;
    JsonPath jsonPath;

    @BeforeClass
    public void loadJsonFile() throws IOException {
        // Read the json from file and store in a String
        exampleJson = new String(Files.readAllBytes(Paths.get("src/test/resources/json-path-example.json")));
        // Create a JsonPath from the input String
        jsonPath = new JsonPath(exampleJson);
    }

    @Test
    public void jsonPathExamples() {
        System.out.println("Nested Value of store.bicycle - n" + jsonPath.get("store.bicycle"));
        System.out.println("nAccessing the Array Element - store.book[0]- n" + jsonPath.get("store.book[0]"));
        System.out.println("nAccessing the inner property of an array Element - store.book[0]- n" + jsonPath.get("store.book[0].author"));
    }
}

Output

Nested Value of store.bicycle -
{color=red, price=19.95}

Accessing the Array Element - store.book[0]-
{author=Nigel Rees, price=8.95, category=reference, title=Sayings of the Century}

Accessing the inner property of an array Element - store.book[0]-
Nigel Rees

What is XmlPath in Rest Assured?

Rest apis can also return the response in xml format. we can parse the XML and compare the objects but what if we want to compare only specific property or attribute of the XML?
XmlPath is used to get the value of any particular property or attribute form the XML.
Lets dive into the XmlPath and understand different scenarios to get the values for a specific property.

We have saved the below XML into xml-path-example.xml file –

<?xml version="1.0" encoding="UTF-8" ?>
<store>
    <book>
        <category>reference</category>
        <author>Nigel Rees</author>
        <title>Sayings of the Century</title>
        <price>8.95</price>
    </book>
    <book>
        <category>fiction</category>
        <author>Evelyn Waugh</author>
        <title>Sword of Honour</title>
        <price>12.99</price>
    </book>
    <book>
        <category>fiction</category>
        <author>Herman Melville</author>
        <title>Moby Dick</title>
        <isbn>0-553-21311-3</isbn>
        <price>8.99</price>
    </book>
    <book>
        <category>fiction</category>
        <author>J. R. R. Tolkien</author>
        <title>The Lord of the Rings</title>
        <isbn>0-395-19395-8</isbn>
        <price>22.99</price>
    </book>
    <bicycle>
        <color>red</color>
        <price>19.95</price>
    </bicycle>
</store>

Lets find out below properties form above json –

  1. the bicycle information – store.bicycle
  2. First book in the list – store.book[0]
  3. Author of the first book – store.book[0].author
package onlyfullstack.verifyResponse;

import io.restassured.path.xml.XmlPath;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class XmlPathExample {

    String exampleXml;
    XmlPath xmlPath;

    @BeforeClass
    public void loadJsonFile() throws IOException {
        exampleXml = new String(Files.readAllBytes(Paths.get("src/test/resources/xml-path-example.xml")));
        xmlPath = new XmlPath(exampleXml);
    }

    @Test
    public void xmlPathExamples() {
        System.out.println("Nested Value of store.bicycle - n" + xmlPath.get("store.bicycle"));
        System.out.println("nAccessing the Array Element - store.book[0]- n" + xmlPath.get("store.book[0]"));
        System.out.println("nAccessing the inner property of an array Element - store.book[0]- n" + xmlPath.get("store.book[0].author"));
    }
}

Output

Nested Value of store.bicycle -
red19.95

Accessing the Array Element - store.book[0]-
referenceNigel ReesSayings of the Century8.95

Accessing the inner property of an array Element - store.book[0]-
Nigel Rees

Lets go to our next tutorial where we will discuss below points :

Part 8 – Getting and Verifying Response in Rest Assured

Different ways to Verify the Response in Rest Assured
– 1. Verify the Response with JsonPath in Rest Assured
– 2. Extracting the response and Validating with assertion
– 3. Validating the response with Json String in Rest Assured
– 4. Validating the response from file in Rest Assured
– 5. Validating the response with Json Schema in Rest Assured
– What is Json Schema?
– How to generate the Json Schema?
https://www.onlyfullstack.com/getting-and-verifying-response-in-rest-assured/

Source Code
You can find the complete source code on below GitHub repository – 
https://github.com/onlyfullstack/rest-assured-tutorial

Rest Assured Tutorial
https://www.onlyfullstack.com/rest-assured-tutorial-for-beginners/