java - How to assert a JSON node has child nodes with certain values assigned to them? -
i have json string following:
json = "{\"things\": \n" + " {\"thing\": {\n" + " \"id\":\"123\",\n" + " \"name\":\"yet thing\",\n" + " \"price\":\"$12.99\",\n" }\n" + " }\n" + "}";
is there way can assert id
of thing
123
and it's name
"yet thing
" in the same statement/assert?
at moment, seem fail using filters:
jsonpath.read(json, "$.things.thing[?(@.id == '123')].name")
i following exception:
java.lang.illegalargumentexception: invalid container object
is maybe because thereis no array notation []
in json string above? should there be?
on related note, there introduction using hamcrest (with json assert)? know official tutorial, seem wrong...
update: rational was: if several thing
elements back, order have no guarantee (so can't match thing[1]
(unless looped through them all))? how make sure 1 element has both, right id , right name? if check children separately, don't run risk 1 thing
has right name , right id, none has both? (would possible json format, or have array in case anyway, "thing": [ { ... }, { ... } ], ...
?
p.s.: tried use jsonpath above follows in end: assertequals("yet thing", jsonpath.read(json, "$.things.thing[?(@.id == '123')].name"));
that's exception might have come from, too. also, asked question on jsonpath mailing list, didn't replies far, hoping might here quicker... :)
the tutorial gives solution problem, seems:
jsonassert.with(json).assertthat("$.things.thing.id", matchers.equalto("123")) .assertthat("$.things.thing.name", matchers.equalto("yet thing"));
Comments
Post a Comment