问题
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