</>

Technology

Rest Assured

Difficulty

Beginner

Interview Question

What is JSON vs XML in API testing and what are the differences?

Answer

JSON vs XML in API Testing

JSON (JavaScript Object Notation)

JSON is a lightweight, human-readable data format using key-value pairs.

JSON
{
  "user": {
    "id": 1,
    "name": "John Doe",
    "email": "john@example.com",
    "roles": ["admin", "editor"],
    "active": true
  }
}

XML (eXtensible Markup Language)

XML is a self-describing markup language using tags.

XML
<?xml version="1.0" encoding="UTF-8"?>
<user>
  <id>1</id>
  <name>John Doe</name>
  <email>john@example.com</email>
  <roles>
    <role>admin</role>
    <role>editor</role>
  </roles>
  <active>true</active>
</user>

Comparison

FeatureJSONXML
ReadabilityEasier to readMore verbose
SizeSmaller payloadLarger payload
Parsing speedFasterSlower
Browser supportNativeNeeds XML parser
Data typesString, number, boolean, array, objectStrings only (need schema for types)
CommentsNot supportedSupported
AttributesNo attributesSupports attributes
Used inREST APIsSOAP, some legacy REST
Schema validationJSON SchemaXSD (XML Schema Definition)

Parsing XML in Rest Assured

Java
// XML response — use XmlPath
given()
    .header("Accept", "application/xml")
    .when()
    .get("/api/users/1")
    .then()
    .statusCode(200)
    .body("user.name", equalTo("John Doe"))
    .body("user.email", equalTo("john@example.com"));

// Extract using XmlPath
XmlPath xmlPath = given()
                      .when()
                      .get("/api/users/1")
                      .then()
                      .extract()
                      .xmlPath();

String name = xmlPath.getString("user.name");

Parsing JSON in Rest Assured

Java
// JSON response — use JsonPath
given()
    .when()
    .get("/api/users/1")
    .then()
    .statusCode(200)
    .body("user.name", equalTo("John Doe"))
    .body("user.roles[0]", equalTo("admin"));

In Interviews

"Most modern REST APIs use JSON because it's lightweight, fast to parse, and natively supported by JavaScript. XML is still used in enterprise SOAP services and some legacy systems. When testing, Rest Assured supports both — JsonPath for JSON and XmlPath for XML."

Follow AutomateQA

Related Topics