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
| Feature | JSON | XML |
|---|---|---|
| Readability | Easier to read | More verbose |
| Size | Smaller payload | Larger payload |
| Parsing speed | Faster | Slower |
| Browser support | Native | Needs XML parser |
| Data types | String, number, boolean, array, object | Strings only (need schema for types) |
| Comments | Not supported | Supported |
| Attributes | No attributes | Supports attributes |
| Used in | REST APIs | SOAP, some legacy REST |
| Schema validation | JSON Schema | XSD (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."
