Sathish
Sathish

Reputation: 360

How to replace a key with def value using karate framework

I have a JSON file as given below:

{
    "lastname": {
        "displayName": "lastname"
    },
    "#(dynamicKey)": {
        "displayName": "#(dynamicKey)"
    }
}

When I try to read the file, the key and values are not getting updated but when I use JSON like below, value got replaced by the def values. If I give dynamic key both key and value are not getting updated. :-(

{
    "lastname": {
        "displayName": "lastname"
     },
     "someKey": {
        "displayName": "#(dynamicKey)"
     }
}

Could you please help me on how to replace dynamic key and value?

Upvotes: 1

Views: 1838

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58088

This is how you can handle dynamic keys. The #(dynamicKey) embedded-expression approach will not work.

* def dynamicKey = 'bar'
* def test = { someKey: 'foo' }
* test[dynamicKey] = 'baz'
* match test == { someKey: 'foo', bar: 'baz' }

Upvotes: 1

Related Questions