# How to avoid escaping characters in Prolog strings

**URL:** <https://swi-prolog.discourse.group/t/how-to-avoid-escaping-characters-in-prolog-strings/2827>\
**Category:** Nice to know\
**Created:** [August 26, 2020, 12:38pm UTC](https://swi-prolog.discourse.group/t/how-to-avoid-escaping-characters-in-prolog-strings/2827 "2020-08-26T12:38:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [August 26, 2020, 12:38pm UTC](https://swi-prolog.discourse.group/t/how-to-avoid-escaping-characters-in-prolog-strings/2827/1 "2020-08-26T12:38:44Z")

</div>

For those with knowledge of ancient coding one is familiar with the problem of trying to represent a [string literal](https://en.wikipedia.org/wiki/String_literal) containing a string within the string.

My personal favorite solution to the problem is the [triple quoted string](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/strings#triple-quoted-strings). So often when I need to put a string within a string and format it nicely as input with SWI-Prolog I search for [SWI-Prolog triple quoted string](http://8) and then remember that the answer is not to be found that way.

Recently having the need to use JSON which is lots of strings on multiple lines, it would be nice to have something like triple quotes strings. If one knows that many of the SWI-Prolog predicates that accept strings also accept atoms, one can pass the value along as an atom that contains a string, e.g.

```prolog
' "I am a string in an atom" '

```

* * *

The JSON data examples

Linear

```prolog
{ "nodes": [], "edges": [] }

```

Multi-line

```prolog
{
	"nodes": [],
	"edges": []
}

```

Examples:

```prolog
% Notice the escape charters needed when the input value is a string.
example_string_linear :-
    open_string("{ \"nodes\": [], \"edges\": [] }",Stream),
    json_read_dict(Stream,Dict,[default_tag(graph)]),
    format('Dict: ~w~n',[Dict]).

example_atom_linear :-
    open_string('{ "nodes": [], "edges": [] }',Stream),
    json_read_dict(Stream,Dict,[default_tag(graph)]),
    format('Dict: ~w~n',[Dict]).

% Notice the escape charters needed when the input value is a string.
example_string_multiline :-
    open_string("\c
        {\n\c
          \"nodes\":\n\c
            [],\n\c
          \"edges\":\n\c
          []\n\c
        }",Stream),
    json_read_dict(Stream,Dict,[default_tag(graph)]),
    format('Dict: ~w~n',[Dict]).

example_atom_multiline :-
    open_string('
            {
                "nodes": [],
                "edges": []
            }'
        ,Stream),
    json_read_dict(Stream,Dict,[default_tag(graph)]),
    format('Dict: ~w~n',[Dict]).

example_real_world :-
    open_string('
    {
        "nodes": [
            {
                "data": {
                    "id": "a",
                    "name": "Glucose",
                    "image": "glucose.svg"
                }
            },
            {
                "data": {
                    "id": "b",
                    "name": "2pg",
                    "image": "2pg.svg"
                }
            }
        ],

        "edges": [
            {
                "data" : {
                    "id": "ab",
                    "source": "a",
                    "target": "b"
                }
            }
        ]
    }'
    ,Stream),
    json_read_dict(Stream,Dict,[default_tag(graph)]),
    format('Dict: ~w~n',[Dict]).

```

Clearly when the input is an atom and on multiple lines it is the most easy to comprehend. Also it is a cut and paste version of valid JSON, so no need to convert/add escape characters.  
See: [JSONLint](https://jsonlint.com/) - The JSON Validator

Example output:

```prolog
?- example_string_linear.
Dict: graph{edges:[],nodes:[]}
true.

?- example_atom_linear.
Dict: graph{edges:[],nodes:[]}
true.

?- example_string_multiline.
Dict: graph{edges:[],nodes:[]}
true.

?- example_atom_multiline.
Dict: graph{edges:[],nodes:[]}
true.

?- example_real_world.
Dict: graph{edges:[graph{data:graph{id:ab,source:a,target:b}}],nodes:[graph{data:graph{id:a,image:glucose.svg,name:Glucose}},graph{data:graph{id:b,image:2pg.svg,name:2pg}}]}
true.

```

* * *

> **Personal notes**
>
> TODO: [Quasi Quotation](https://www.swi-prolog.org/pldoc/man?section=quasiquotations) might also be a means and even a better means.
