1
 [
      {
         "fieldName": "fieldOne",
         "normalJson": "{\"nameRawText\":\"Man\",\"addressRawText\":\"23 Third \",\"cityRawText\":\"LONDON\",\"stateRawText\":null,\"countryRawText\":\"United Kingdom\",\"rawText\":\"Man 23 Third, LONDON United Kingdom\"}",
         "documentName": "documentOne"
     },
    {
         "fieldName": "fieldTwo",
         "normalJson": "{\"nameRawText\":\"Man\",\"addressRawText\":\"23 Third SE55\",\"cityRawText\":\"LONDON\",\"stateRawText\":null,\"countryRawText\":\"United Kingdom\",\"rawText\":\"Man 23 Third, LONDON United Kingdom\"}",
         "documentName": "documentOne"
     },
     {
         "fieldName": "fieldThree",
         "normalJson": "{\"nameRawText\":\"Mick\",\"addressRawText\":null,\"cityRawText\":null,\"stateRawText\":null,\"countryRawText\":\"India\",\"rawText\":\"Mick India\"}",
         "documentName": "documentOne"
     },
      {
         "fieldName": "fieldSix",
         "normalJson": "{\"nameRawText\":\"Mick\",\"addressRawText\":null,\"cityRawText\":null,\"stateRawText\":null,\"countryRawText\":\"India\",\"rawText\":\"Mick India\"}",
         "documentName": "documentOne"
     },
     {
         "fieldName": "fieldOne",
         "normalJson": "{\"nameRawText\":\"Man\",\"addressRawText\":\"23 Third \",\"cityRawText\":\"LONDON\",\"stateRawText\":null,\"countryRawText\":\"United Kingdom\",\"rawText\":\"Man 23 Third, LONDON United Kingdom\"}",
         "documentName": "documentTwo"
     },
     {
         "fieldName": "fieldFour",
         "normalJson": "{\"nameRawText\":\"Man\",\"addressRawText\":\"23 Third \",\"cityRawText\":\"LONDON\",\"stateRawText\":null,\"countryRawText\":\"United Kingdom\",\"rawText\":\"Man  23 Third, LONDON United Kingdom\"}",
         "documentName": "documentTwo"
     },
     ]

Expected output:

[
{
"nameRawText" : "Man"
"fieldName" : "fieldOne,fieldTwo,fieldFour"
"documentName" : documentOne,documentTwo"
"nameRawText" : "Mick"
"fieldName" : "fieldThree,fieldSix"
"documentName" : "documentOne"
}
]

Test Scenario:

Find duplicate values of (nameRawText, cityRawText or countryRawText) inside "normalJson" and print duplicate values along with the respective keys (nameRawText, cityRawText or countryRawText) + "fieldName" + "documentName".

Query:

Please provide suggestions to handle the above mentioned scenario dynamically.

Please note the following points:

  1. Payload is dynamic with various number of documents and fields.
  2. We tried (karate.filter, karate.jsonPath, Hashmap) and did not receive expected output
  3. karate.distinct method is not supported in my IDE.
James Z
  • 12,209
  • 10
  • 24
  • 44
Srilakshmi
  • 23
  • 2

1 Answers1

0

Here is the solution. Take some time to understand each step:

* def step1 = response.map(x => ({ documentName: x.documentName, fieldName: x.fieldName, normalJson: karate.fromString(x.normalJson) }))
* def allNames = step1.map(x => x.normalJson.nameRawText)
* def names = karate.distinct(allNames)
* def fun = 
"""
function(x) {
  let filtered = step1.filter(y => y.normalJson.nameRawText == x);
  let fields = filtered.map(y => y.fieldName);
  let documents = filtered.map(y => y.documentName);
  return { name: x, fieldNames: fields.join(','), documentNames: documents.join(',')};
}
"""
* def result = names.map(fun)
* print result

Which prints:

[
  {
    "name": "Man",
    "fieldNames": "fieldOne,fieldTwo,fieldOne,fieldFour",
    "documentNames": "documentOne,documentOne,documentTwo,documentTwo"
  },
  {
    "name": "Mick",
    "fieldNames": "fieldThree,fieldSix",
    "documentNames": "documentOne,documentOne"
  }
]

Refer this answer to understand the concepts. Take the help of someone who knows JS if needed: https://stackoverflow.com/a/76091034/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    Hi Peter, Thanks for providing solution. As mentioned in question, karate.distinct method is not supported in my system's IDE since system does not allow to add beyond 1.1.0.RC2 dependency Requesting you to guide without karate.distinct method in the steps – Srilakshmi Jun 05 '23 at 05:21
  • @Srilakshmi sorry my recommendation is that you upgrade and we don't support old versions. you can see if this answer helps, but the rest is up to you: https://stackoverflow.com/a/53045121/143475 - also read this: https://stackoverflow.com/help/someone-answers – Peter Thomas Jun 05 '23 at 06:02
  • sorry for late response, I have updated the Karate dependency to 1.3.0 and receiving exception: "org.graalvm.polyglot.PolyglotException: TypeError: Cannot read property "nameRawText" from null" upon running the java script code. Please provide guidance. – Srilakshmi Aug 22 '23 at 12:28
  • @Srilakshmi guidance will be provided if you follow this process: https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue – Peter Thomas Aug 22 '23 at 13:25
  • I have raised an issue and kindly refer to -> https://github.com/karatelabs/karate/issues/2387 – Srilakshmi Aug 25 '23 at 18:13
  • @Srilakshmi I have answered and you really need to take the help of someone who knows JS. by the way, I never recommend these kinds of complicated processing in tests, you should just stick to expected hard-coded responses as far as possible, refer: https://stackoverflow.com/a/54126724/143475 – Peter Thomas Aug 26 '23 at 03:14
  • 1
    your hint resolved the issue, thank you. I appreciate the prompt responses and will incorporate your recommendation on the hard corded responses for the possible scenarios. – Srilakshmi Aug 27 '23 at 08:16