Said Yusifli
Said Yusifli

Reputation: 190

how to manage variable inside of json value string?

I have variable cardholder in my karate-config file.

I assigned it to the new entrID variable.

enter image description hereThe main thing that i am building JSON as a String..

 * def entrID = cardholder  
 * def requestContactHistoryAdd = 
   """ 
   { 
   "RequestBody": "{ \"ENTR_ID\" : \"entrID\", \"BHVR_ID\" : \"VRU\", }"
   } 
   """

Now how can i provide it inside of my json RequestBody?

Upvotes: 1

Views: 409

Answers (2)

Peter Thomas
Peter Thomas

Reputation: 58058

EDIT: since you seem to have a very badly designed API where the JSON has an embedded string (which looks like JSON).

Please note I am using a string type below: https://github.com/intuit/karate#type-conversion

You can do this:

* def entrID = 'foo'  
* string temp = { "ENTR_ID" : "#(entrID)", "BHVR_ID" : "VRU" }
# note that you could have done this: 
# def temp = '{ "ENTR_ID" : "' + entrID + '", "BHVR_ID" : "VRU" }'
* def body = { RequestBody: '#(temp)' }
* print body

Which gives you:

08:17:25.671 [main] INFO  com.intuit.karate - [print] {
  "RequestBody": "{\"ENTR_ID\":\"foo\",\"BHVR_ID\":\"VRU\"}"
}

Upvotes: 1

Said Yusifli
Said Yusifli

Reputation: 190

i solved it also like this

 * def entrID = someValueFromSomeWhere
 * def bodyValue = "{ \"ENTR_ID\":\"" + entrID + "\", \"BHVR_ID\" : \"VRU\" }"
 * def requestContactHistoryAdd = 
   """ 
   { 
   "RequestBody": "#(bodyValue)"
   } 
   """

we can also do this way

 * def bodyValue = "{ \"ENTR_ID\":\"" + someValueFromSomeWhere + "\", \"BHVR_ID\" : \"VRU\" }"
 * def requestContactHistoryAdd = 
  """
  { 
  "RequestBody": "#(bodyValue)" 
  }
  """

Upvotes: 1

Related Questions