Matching JSON
equalToJson
and matchesJsonPath
, which are described
in detail in this article.
equalToJson
match operator performs a semantic comparison of the input JSON
against the expected JSON. This has a number of advantages over a straight string
comparison:
equalToJson
will match only if all of the elements in the input JSON
are the same as the expected JSON, arrays are in the same order and no additional
attributes are present.
For instance, given an expected JSON document like
sizes
order would cause a non-match:
${json-unit.ignore}
, the element’s type is also ignored (in addition to its value),
so in the above case a string, boolean etc. could have been used in place of the numeric ID.
If you want to constrain an element to a specific type but still ignore the value
you can use one of the following placeholders:
${json-unit.regex}[A-Z]+
(any Java-style regular expression can be used)${json-unit.any-string}
${json-unit.any-boolean}
${json-unit.any-number}
matchesJsonPath
equalTo
, matches
etc.).
Given the following configration:
$.sizes[1]
equal to
M
would match:
$.addresses[?(@.type == 'business')].postcode
contains
N11NN
would match:
contains
in this instance as a JSONPath expression containing
a query part (between the [?
and ]
) will always return a collection
of results.
Matching an element found recursively.
$..postcode
contains
N11NN
would match: