Search code examples
automationkarate

Replacing variables in JSON with string text in Karate DSL


I have a json request body which contains string text where some variable value need to be replaced but there is no way directly through which it can be replaced in json.

Example: { "key1":"#(var1)", "key2":"https://example.com/123/search" }

I need to replace value 123 in key2, but if i am doing "https://example.com/"+"#(var2)"+"/search" it is not working out. Tried few other stuff but nothing working out in case of karate replace variable.


Solution

  • Just use JS:

    * def body = { key2: 'https://example.com/123/search' }
    * body.key2 = body.key2.replace('123', '456')
    * match body == { key2: 'https://example.com/456/search' }