Home > Blockchain >  Karate: How to create a json object/map from 2 arrays and merge them?
Karate: How to create a json object/map from 2 arrays and merge them?

Time:10-15

I am having 2 arrays

array1 = ['a','b']

array2 = [1,2]

I want to merge these 2 arrays and convert them to map like below:

 [
      {
        "firstparam": 'a'
        "secondparam": 1
      },
      {
        "firstparam": 'b'
        "secondparam": 2
      }
    ]

I am trying this code:

* def map1 = array1
* def map1 = karate.mapWithKey(map1, 'firstparam')
* def map2 = array2
* def map2 = karate.mapWithKey(map2, 'secondparam')

this code is creating map1 & map2. now I want to merge these 2 maps in the above format. how to do it?

basically, i want to send this map to a feature file which is expected 2 parameters.

* def result = karate.call('*.feature', map)

'*.feature' is expecting 2 parameters per call i.e, firstparam & secondparam

CodePudding user response:

Here you go:

* def array1 = ['a', 'b']
* def array2 = [1, 2]
* def array3 = array1.map((x, i) => ({ firstparam: x, secondparam: array2[i] }))
* match array3 == [{ firstparam: 'a', secondparam: 1 }, { firstparam: 'b', secondparam: 2 }]
  • Related