How to set a dynamic value for a json array in Karate

*爱你&永不变心* 提交于 2020-06-26 12:24:58

问题


i have a json file, which needs uniq values for each post request. uniqId = '55555'. for all the uniq ids i need to pass same uniqId. so far i was able to only set for only the endtoEndid. unable to set for invoiceNum and txnValue. Please help. so far i have done following:

  • eval req = {"endtoEndid":"", "prgrmId":"2344", "invoices": [ { "invoiceNum":""}], "currency":5.99, "txnRefs": [ { "txnId":"name", "txnValue":""},{ "txnId":"state", "txnValue":"illinois"}]}

  • eval req['endtoEndId'] = 'endtoEnd' + uniqId. (successfull)

  • eval req.invoices['invoiceNum'] = 'invoice' + uniqId (not successful)
  • eval req['txnRefs.txnValue]'] = 'txn' + uniqId (not successful) And request req

then i tried splitting:

  • def reqInvoices = req.invoices
  • def reqTxnRefs = req.txnRefs[0]

  • eval reqInvoices['invoiceNum'] = 'invoice' + uniqId (not successful)

  • eval reqTxnRefs['txnValue'] = 'txn' + uniqId (not successful) And request req

i need following result:

{"endtoEndid":"endToEndIduniqId", "prgrmId":"2344", "invoices": [ { "invoiceNum":"invoiceuniqId"}], "currency":5.99, "txnRefs": [ { "txnId":"name", "txnValue":"txnuniqId"},{ "txnId":"state", "txnValue":"illinois"}]}

Thank you


回答1:


Please try the latest version or 0.9.6.RC3.

Setting JSON is much easier now, you don't need any eval or set.

Example:

* def foo = {}
* foo.bar = 'baz'
* def random = function(){ return java.lang.System.currentTimeMillis() + '' }
* foo.ban = random()
* print foo

Which gives:

{
  "bar": "baz",
  "ban": "1591756622099"
}


来源:https://stackoverflow.com/questions/62289302/how-to-set-a-dynamic-value-for-a-json-array-in-karate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!