2

I would like to convert each request to the following format before the request is sent.

"Store": {
"Tesco": "1284756",
    "Sainsburys": "37384657",
    "Asda": "1234567"
}

I believe that we can use something like JSON.stringify({"Store": {"Tesco": "1284756","Sainsburys": "37384657","Asda": "1234567"}}).replace(/[\"]/g, '\\\"'); to convert the string.

How can we use this in Karate to convert each request into the following format:

\"Store\": {\n\"Tesco\": \"1284756\",\n    \"Sainsburys\": \"37384657\",\n    \"Asda\": \"1234567\"\n}\n

I would also like to treat the response and revert it back to the following format?

"Store": {
"Tesco": "1284756",
    "Sainsburys": "37384657",
    "Asda": "1234567"
}
Ravi Makwana
  • 2,782
  • 1
  • 29
  • 41
zwanchi101
  • 181
  • 3
  • 14
  • 2
    Why do you want to escape all the quotes and produce invalid JSON? –  Feb 03 '20 at 14:50
  • Does this answer your question? [How to escape special characters in building a JSON string?](https://stackoverflow.com/questions/19176024/how-to-escape-special-characters-in-building-a-json-string) – Ravi Makwana Feb 03 '20 at 15:00

1 Answers1

0

Please avoid JSON.stringify() and use the "type conversion" features in Karate designed specifically for this. And read the docs:

https://github.com/intuit/karate#type-conversion

* json temp = someString
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Thanks for your answer Peter, this would help convert the special characters to normal json. However, I would also like to convert normal json to include the special characters before the request is sent. This is because the system under test requires the special characters in the request – zwanchi101 Feb 04 '20 at 16:09
  • @zwanchi101 I honestly think you are confused. Karate sends proper JSON. if your server cannot handle it I would consider it a bug in the server. anyway, if you insist, just convert the JSON to a string, and run a custom replace command (search the readme) because the slashes and line breaks in your sample is specific to your environment. feel free to wait for a second opinion from someone else, I have no other suggestions – Peter Thomas Feb 04 '20 at 16:19