There may be a built-in way to do this, but I couldn't find it.
This macro will escape a JSON string, either on the clipboard, or via a passed parameter. Example:
Input:
Called "JSON Escape String" without passing a parameter,
so the macro escaped this text, which was on the clipboard.
Output:
Called \"JSON Escape String\" without passing a parameter,\nso the macro escaped this text, which was on the clipboard.
Click for Macro Image
You can call it like this and it escapes what's on the clipboard:
Or like this, and the result is returned in Instance_Result:
JSON Escape String.v1.0.kmmacros (16.2 KB)
It includes a couple of example macros.