# Escaping strings for output to JSON file

**URL:** <https://discourse.cmake.org/t/escaping-strings-for-output-to-json-file/5297>\
**Category:** Code\
**Created:** [March 25, 2022, 5:44pm UTC](https://discourse.cmake.org/t/escaping-strings-for-output-to-json-file/5297 "2022-03-25T17:44:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dwarwick](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dwarwick/32/2298_2.png) [@dwarwick](https://discourse.cmake.org/u/dwarwick)\
**Post date:** [March 25, 2022, 5:44pm UTC](https://discourse.cmake.org/t/escaping-strings-for-output-to-json-file/5297/1 "2022-03-25T17:44:30Z")

</div>

I have an existing string in CMake – compile flags passed to the target – that have quotes in them. If I want to write these to a JSON file, I need to escape the quotes in them. Since these are not defined manually by me in CMake, I can’t manually escape them. I could do a string replace of " with ", but I want to avoid massaging any parsing with regex/string replace in favor of a proper escaping API. Is there a way I can properly escape these strings so that they can be written to a JSON file?

---

<div class="post-metadata">

**Author:** ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)\
**Post date:** [March 26, 2022, 9:45am UTC](https://discourse.cmake.org/t/escaping-strings-for-output-to-json-file/5297/2 "2022-03-26T09:45:18Z")

</div>

Are you aware of the JSON functionality introduced in CMake 3.19?

[https://cmake.org/cmake/help/latest/command/string.html#json](https://cmake.org/cmake/help/latest/command/string.html#json)

---

<div class="post-metadata">

**Author:** ![tniessen](https://discourse.cmake.org/user_avatar/discourse.cmake.org/tniessen/32/2305_2.png) [@tniessen](https://discourse.cmake.org/u/tniessen)\
**Post date:** [March 28, 2022, 6:34pm UTC](https://discourse.cmake.org/t/escaping-strings-for-output-to-json-file/5297/3 "2022-03-28T18:34:29Z")

</div>

This appears to be related to [issue 22252](https://gitlab.kitware.com/cmake/cmake/-/issues/22252). I believe CMake does not provide a straightforward way to properly escape string variables for use with JSON.

You might find [the `json_esc` function from this gist](https://gist.github.com/tniessen/77bd09fca3c15104d4bdfb4ec9f4ba1c) useful as a workaround for CMake 3.19 and above. Please let me know if you find a bug.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [March 29, 2022, 1:22pm UTC](https://discourse.cmake.org/t/escaping-strings-for-output-to-json-file/5297/4 "2022-03-29T13:22:44Z")

</div>

There is `string(CONFIGURE "${input}" output @ONLY ESCAPE_QUOTES)` that could be (ab)used (if `@var@` doesn’t appear in the flags).

Note that `\u` and other escapes won’t be handled though, so Windows paths may need some special care.
