Murugesh Subramaniam
Murugesh Subramaniam

Reputation: 313

How to set dynamic value as a key to json string in request

In the below JSON request: value for "paid" is dynamically generated. When I try to pass the value to JSON string attribute, instead of dynamic value to replace "paid" it is taking "paid" as a string.

{
  "ENCRYP": {
    "data": "ENCRYPTED",
    "tEncryptedP": {
      "#(paid)": {
        "encryptedP": "#(parameters)"
      }
    }
  }
}

How to pass a dynamic value to JSON string attribute in this case?

Upvotes: 2

Views: 1002

Answers (1)

Babu Sekaran
Babu Sekaran

Reputation: 4239

It looks like you are trying to set a dynamic key using an embedded expression,

not sure if it is possible to set a new key using an embedded expression.

but you can try something like this,

* def paid = "PaidKey"
* def parameters = "PAID PARAMETERS"
* def enc = 
"""
{
  "ENCRYP": {
    "data": "ENCRYPTED",
    "tEncryptedP": {
    }
  }
}
"""
* def encryptedP = {"encryptedP": "#(parameters)"}
* eval enc.ENCRYP.tEncryptedP[paid] = encryptedP 

Upvotes: 3

Related Questions