Karate Framework; Reuse array of the values to paste the values into another json with N nested elements

Viewed 30

Hi Team! Here is my question - how to reuse the array of the values from json1 and paste the values one by one into a json2; and create JSON with 1 parent and nested elements

json1;

[
  "EAID_6F41E794_9FE8_447a_9AA2_BD5CA941B0A8",
  "EAID_7144A90E_3566_43e1_A071_9D9977B99E8A",
  "EAID_14F85DAB_7BF7_4ad1_8DBF_F2ABEBE7E35A",
  "EAID_DEDC0908_FDE9_4384_8DED_B9FF30760C6C",
  "EAID_6F41E794_9FE8_447a_9AA2_BD5CA941B0A8",
  "EAID_14F85DAB_7BF7_4ad1_8DBF_F2ABEBE7E35A",
  "EAID_6F41E794_9FE8_447a_9AA2_BD5CA941B0A8",
  "EAID_7144A90E_3566_43e1_A071_9D9977B99E8A",
  "_19_0_3_8aa01e4_1610562779046_96927_69106",
  "_19_0_3_8aa01e4_1610562779046_96927_69106"
]
  {
    "orders": [
      {
            "values": [
              "EAID_6F41E794_9FE8_447a_9AA2_BD5CA941B0A8"
            ]
      },
          {
            "values": [
              "EAID_7144A90E_3566_43e1_A071_9D9977B99E8A"
            ]
          }
        ]
      }
1 Answers

Here you go:

* def fun = function(x){ return { values: [ x ] } }
* def result = karate.map(json1, fun)
* def json2 = { orders: '#(result)' }
* print json2

Please refer the docs: https://github.com/karatelabs/karate#json-transforms

Just for fun, here's an alternate more concise solution:

* def fun = x => ({ values: [ x ] })
* def json2 = ({ orders: json1.map(fun) })
* print json2

Simpler version:

Related