简体   繁体   中英

karate javaScript array format

Here is my javascript which prepares json which I will use for matching an api response

var squArray = []
for (j = loopincri; j < (loopincri + skuCount); j++) {
    var skuJson = {
        "skuCode": skuCodes[j],
        "quantity": quantity
    }
    squArray.push(skuJson)
}

when I am printing this is coming like:

{
  "0": {
    "sku_code": "50",
    "quantityNO": 2,
    "min": 550,
    "max": 13000,
    "dtePrice": {
      "0": {
        "date": "2019-04-11",
        "listPrice": 6600,
        "salePrice": 3870
      },
      "1": {
        "date": "2019-04-12",
        "listPrice": 6600,
        "salePrice": 3870
      }
    }
  }
}

But I want the result to be like

[
  {
    "sku_code": "50",
    "quantityNO": 2,
    "min": 550,
    "max": 13000,
    "dtePrice": [
      {
        "date": "2019-04-11",
        "listPrice": 6600,
        "salePrice": 3870
      },
      {
        "date": "2019-04-12",
        "listPrice": 6600,
        "salePrice": 3870
      }
    ]
  }
]

Can you please help me out with this? As the response of the api is in the format I want

Yes there is an issue with arrays if you do too much in JS: https://stackoverflow.com/a/54256766/143475 - so if you do karate.read() to create the chunks of JSON, it should start working.

My recommendation is avoid using JS as far as possible, try to use Karate natives for looping and transformation etc. For example:

* def skuCodes = ['a', 'b', 'c', 'd', 'e']
* def nums = []
* eval for(var i = 0; i < 5; i++) nums.add(~~i)
* def fun = function(x){ return { skuCode: skuCodes[x], array: read('array.json') } }
* def json = karate.map(nums, fun)

Yes, using read() is a slight inconvenience, but note that you can use embedded expressions within a JSON file. This will be fixed in a future version of Karate.

This could be workaround for overcoming that jdk bug discussed in https://stackoverflow.com/a/54256766/8615449

Instead of returning your object directly, try,

return Java.type("com.intuit.karate.JsonUtils").toJson(squArray)

I know this return JSON in a string format, you can cast it in your feature by

* json myJson = JsFunction()

here JsFunction() is the function that you created above which returns that json value.

Don't know about karate JS. But if karate JS is converting your array to object then you can reconvert to array. As you can see that the resulting object is arraylike which can easily be converted to array using "Array.from(queryObject)" method. Modification :-

var squArray = []
for(j=loopincri ; j<(loopincri+skuCount); j++){
        var skuJson = {"skuCode":skuCodes[j],
                       "quantity":quantity};
        squArray.push(skuJson);
    }
let squArray = Array.from(squArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM