问题
I am wondering if it is possible to change all occurrences of a text in a Json response from a Karate DSL text without having a placeholder. For example , the fragment "https://thisisthepart.tochange" appears many times in the json response of karate dsl. I cannot do it with "set" because i do not know for which keys is going to appear that text.
"imageUrl": ["https://thisisthepart.tochange/idontwannachange/thispart"]
Thanks in advance
回答1:
Yes, convert to a string, use the Java API String.replace()
and re-convert:
* string response = response
* json response = response.replace('foo', 'bar')
Also refer: https://github.com/intuit/karate#type-conversion
EDIT: note that you can "re-build" JSON using a loop:
* def before = { a: 'hello', b: 'world' }
* def after = {}
* def fun = function(k, v){ after[k] = v + 'edit' }
* karate.forEach(before, fun)
* match after == { a: 'helloedit', b: 'worldedit' }
来源:https://stackoverflow.com/questions/65182252/how-can-you-change-all-appearances-of-a-text-response-in-karate-dsl-without-havi