jadreymis
jadreymis

Reputation: 79

How can I use a variable as a json key in karate?

* def myvariable = 1 
* def schema =
"""
{
  myvariable : '#number',
  2: '#number',
  3: '#number',
  4: '#number',
  5: '#number',
  6: '#number',
}
"""

I need to use 'myvariable' as a key. How can I do this?

Upvotes: 3

Views: 1440

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

Here you go:

* def schema = {}
* schema.myvariable = 1
* match schema == { myvariable: 1 }

# dynamic key name
* def name = 'myvariable'
* def schema = {}
* schema[name] = 1
* match schema == { myvariable: 1 }

Upvotes: 5

Related Questions