1
* def res1 = {"member":{"muid":"MBR1"},"part":[{"PID":"M123"},{"supportedMembers":[{"muid":"MBR3","status":{"code":"A"}},{"muid":"MBR2","status":{"code":"I"}}]}]}

* def res2 = {"members":[{"member":{"muid":"MBR2","test":[{"EID":"E123"}]}},{"member":{"muid":"MBR3","test":[{"EID":"E123"}]}}]}

Karate: Match array elements of two different JSON I have another requirement which is related to my earlier post.

* def id = res1.member.muid

I want to remove id from res2 response, which can be any where in res2.members.member, and do the matching with res1 to see the presence of muids I tried something like below, but its not working:

* karate.remove('$res2.members[*]..muid','$.muid[id]') 
JCK
  • 129
  • 3
  • 11

1 Answers1

2

Sample Code:

Feature: Validation

Scenario:

    * def res1 = {"member":{"muid":"MBR1"},"part":[{"PID":"M123"},{"supportedMembers":[{"muid":"MBR3","status":{"code":"A"}},{"muid":"MBR2","status":{"code":"I"}}]}]}
    * def res2 = {"members":[{"member":{"muid":"MBR2","test":[{"EID":"E123"}]}},{"member":{"muid":"MBR3","test":[{"EID":"E123"}]}}]}
    * def id = res1.member.muid
    * def res2ids = $res2.members[*]..muid
    * def data2 = karate.mapWithKey(res2ids, 'muid')
    * print data2
    * def res2ids = karate.jsonPath(data2, "$[?(@.muid != '" + id+ "')]")
    * def res2ids = $res2ids[*]..muid
    * print res2ids
    * match res1.part[1].supportedMembers[*].muid contains only res2ids
Neodawn
  • 1,086
  • 1
  • 6
  • 9
  • another brilliant answer, thank you Neodawn ! to @JCK who asked the question - I urge you to re-read this answer: https://stackoverflow.com/a/60304887/143475 - please don't have such "over smart" tests, keep them simple. teams that do this kind of stuff struggle later with maintenance. I won't say this again - it is up to you to take my advice or not. peace – Peter Thomas Feb 26 '20 at 19:12
  • Thank you Neodawn. Thanks Peter Thomas. @Peter But I cannot do this `* copy last = child.array1 * remove last[0]` because that is the not the case always. I want to remove only the member thats present in the parent api – JCK Feb 26 '20 at 19:59
  • @JCK I don't think you have understood what I was trying to tell you. By the way, you cannot `remove` using a JsonPath (no `*` or `..` is allowed) – Peter Thomas Feb 27 '20 at 03:00