# Convert dictionary to json dictionary?

**URL:** <https://swi-prolog.discourse.group/t/convert-dictionary-to-json-dictionary/5293>\
**Category:** General\
**Created:** [April 18, 2022, 10:12am UTC](https://swi-prolog.discourse.group/t/convert-dictionary-to-json-dictionary/5293 "2022-04-18T10:12:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Losbarthos](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/losbarthos/32/4037_2.png) [@Losbarthos](https://swi-prolog.discourse.group/u/Losbarthos)\
**Post date:** [April 18, 2022, 10:12am UTC](https://swi-prolog.discourse.group/t/convert-dictionary-to-json-dictionary/5293/1 "2022-04-18T10:12:04Z")

</div>

Based on this minimal .pl-file:

```prolog
:-op(800, fx, ¬).
:-op(801, xfy, ∧).
:-op(802, xfy, ∨).
:-op(803, xfy, →).
:-op(804, xfy, ↔).

```

I am able to define dictionaries of this form:

```prolog
A = proof{1: ¬q, 2:p→q, 3: ¬ (¬p), 4: ¬p, 5:[edge(¬p, ¬p∧ ¬ (¬p)), edge(¬ (¬p), ¬p∧ ¬ (¬p)), rule('∧I')], 6:[edge(¬p, p), edge(¬p∧ ¬ (¬p), p), rule('¬E')], 7:[edge(p, q), edge(p→q, q), rule('→E')], 8:[edge(q, q∧ ¬q), edge(¬q, q∧ ¬q), rule('∧I')], 9:[edge(¬ (¬p), ¬p), edge(q∧ ¬q, ¬p), rule('¬E')]}.

```

Is their a predicate to convert this dictionary in the following string representation (background, to handle with it into other languages like python):

```prolog
B = proof{1:'p→q',2: '¬q',3: '¬ (¬p)',4: '¬p',5:['edge(¬p,¬p∧ ¬ (¬p))','edge(¬ (¬p),¬p∧ ¬ (¬p))','rule("∧I")'],6:['edge(¬p,p)','edge(¬p∧ ¬ (¬p),p)','rule("¬E")'],7:['edge(p,q)','edge(p→q,q)','rule("→E")'],8:['edge(q,q∧ ¬q)','edge(¬q,q∧ ¬q)','rule("∧I")'],9:['edge(¬ (¬p),¬p)','edge(q∧ ¬q,¬p)','rule("¬E")']}.

```

or

```prolog
B ={1:'p→q',2: '¬q',3: '¬ (¬p)',4: '¬p',5:['edge(¬p,¬p∧ ¬ (¬p))','edge(¬ (¬p),¬p∧ ¬ (¬p))','rule("∧I")'],6:['edge(¬p,p)','edge(¬p∧ ¬ (¬p),p)','rule("¬E")'],7:['edge(p,q)','edge(p→q,q)','rule("→E")'],8:['edge(q,q∧ ¬q)','edge(¬q,q∧ ¬q)','rule("∧I")'],9:['edge(¬ (¬p),¬p)','edge(q∧ ¬q,¬p)','rule("¬E")']}.

```

---

<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:** [April 18, 2022, 10:22am UTC](https://swi-prolog.discourse.group/t/convert-dictionary-to-json-dictionary/5293/2 "2022-04-18T10:22:32Z")

</div>

> [@Losbarthos](#):
>
> in the following string representation

`'` by default is an atom not a string, 🙂

e.g.

```prolog
?- atom('p→q').
true.

?- string('p→q').
false.

```

Strings are created with `"` by default.

e.g.

```prolog
?- atom("p→q").
false.

?- string("p→q").
true.

```

Some of these defaults can be changed with [Prolog flags](https://www.swi-prolog.org/pldoc/man?section=flags) but you did not note the use of such so in reading your question I would take that you are using the defaults.

* * *

**EDIT**

In the title you note `json dictionary`, which may lead one to think there is a SWI-Prolog dictionary just for json. AFAIK there is none. JSON can be converted to/from an SWI-Prolog dictionary most of the time and with caveats. See: [Json\_dict/2 - Helpful for learning how to use JSON with SWI-Prolog dict](https://swi-prolog.discourse.group/t/json-dict-2-helpful-for-learning-how-to-use-json-with-swi-prolog-dict/4450)

Also of value: [Sites for testing JSON](https://swi-prolog.discourse.group/t/sites-for-testing-json/2893)

---

<div class="post-metadata">

**Author:** ![Losbarthos](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/losbarthos/32/4037_2.png) [@Losbarthos](https://swi-prolog.discourse.group/u/Losbarthos)\
**Post date:** [April 18, 2022, 10:36am UTC](https://swi-prolog.discourse.group/t/convert-dictionary-to-json-dictionary/5293/3 "2022-04-18T10:36:51Z")

</div>

Thanks for the hint.
