Q5 of 40 · Karate

How do you assert on a JSON response field in Karate?

KarateJuniorkaratematchassertionsfundamentalsapi-testing

Short answer

Short answer: Use * match response.fieldName == 'expected' for a single field, or * match response == { id: '#number', name: 'Alice' } for the whole body. Dot notation navigates nested objects; bracket notation accesses arrays. The match keyword handles deep-equality and fuzzy matching — no assertion library needed.

Detail

Karate's match keyword is the primary assertion mechanism. It handles three cases:

Exact field match:

* match response.name == 'Alice'
* match response.address.city == 'London'
* match response.items[0].sku == 'A1'

Whole-object match — all listed fields must match:

* match response == { id: '#number', name: 'Alice', active: true }

Type/schema markers instead of exact values:

  • '#string' — any string
  • '#number' — any number
  • '#boolean' — any boolean
  • '#uuid' — UUID format
  • '#present' — field exists (any value)
  • '#notnull' — field is not null
  • '#regex \\d{3}' — matches a regex

Partial match (ignores extra fields):

* match response contains { name: 'Alice' }

Unlike REST Assured's Hamcrest matchers, Karate's match does not require imports or a separate library — it is built into the DSL.

// EXAMPLE

user-assertions.feature

Feature: User response assertions

  Scenario: Created user has expected structure
    * url 'https://api.example.com'
    * header Authorization = 'Bearer test-token'
    Given path '/users'
    And   request { name: 'Alice', email: 'alice@example.com' }
    When  method POST
    Then  status 201

    # Exact field assertions
    * match response.name  == 'Alice'
    * match response.email == 'alice@example.com'

    # Type-check — don't care about the actual value
    * match response.id        == '#uuid'
    * match response.createdAt == '#string'
    * match response.score     == '#number'

    # Whole-body structural match
    * match response == {
        id:        '#uuid',
        name:      'Alice',
        email:     'alice@example.com',
        active:    true,
        createdAt: '#string'
      }

// WHAT INTERVIEWERS LOOK FOR

Correct match syntax for both field-level and whole-body assertions, familiarity with the # type markers (#string, #number, #uuid), and knowing match == vs match contains. Candidates who have written real Karate tests use type markers naturally.

// COMMON PITFALL

Using strict == for server-generated values like IDs and timestamps. These values are unknown at test-write time — use #uuid, #number, or #string to express 'a value of this type was returned' rather than coupling to a specific value.