Skip to content

Instantly share code, notes, and snippets.

@ptrthomas
Last active October 19, 2021 21:41
Show Gist options
  • Save ptrthomas/a44579df0806ccd42494781b3832f9d0 to your computer and use it in GitHub Desktop.
Save ptrthomas/a44579df0806ccd42494781b3832f9d0 to your computer and use it in GitHub Desktop.
Karate and JSON `contains`
Given def cat =
"""
{
name: 'Billie',
kittens: [
{ id: 23, name: 'Bob' },
{ id: 42, name: 'Wild' }
]
}
"""
# normal 'equality' match. the wildcard '*' in the json-path returns an array
Then match cat.kittens[*].id == [23, 42]
# 'contains' just checks if the expected items exist
Then match cat.kittens[*].id contains [42, 23]
# and yes, you can assert against nested objects within JSON arrays !
Then match cat.kittens contains [{ id: 42, name: 'Wild' }, { id: 23, name: 'Bob' }]
# ... and even ignore fields at the same time !
Then match cat.kittens contains { id: 42, name: '#string' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment