# Simple Json convention for Prolog

**URL:** <https://swi-prolog.discourse.group/t/simple-json-convention-for-prolog/3724>\
**Category:** Nice to know\
**Created:** [March 17, 2021, 8:11am UTC](https://swi-prolog.discourse.group/t/simple-json-convention-for-prolog/3724 "2021-03-17T08:11:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joeblog](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/joeblog/32/122_2.png) [@joeblog](https://swi-prolog.discourse.group/u/joeblog)\
**Post date:** [March 17, 2021, 8:11am UTC](https://swi-prolog.discourse.group/t/simple-json-convention-for-prolog/3724/1 "2021-03-17T08:11:10Z")

</div>

I’ve come up with the following simple convention for moving between Prolog and Json that Postgresql and web browsers can parse.

```prolog
Terms = [cell(1, 1, x), cell(1, 2, x), cell(1, 3, o),
         cell(2, 1, b), cell(2, 2, o), cell(2, 3, b),
         cell(3, 1, b), cell(3, 2, o), cell(3, 3, x),
         control(white)].

```

```json
Json = [["cell", 1, 1, "x"], ["cell", 1, 2, "x"], ["cell", 1, 3, "o"],
        ["cell", 2, 1, "b"], ["cell", 2, 2, "o"], ["cell", 2, 3, "b"],
        ["cell", 3, 1, "b"], ["cell", 3, 2, "o"], ["cell", 3, 3, "x"],
        ["control", "white"]]

```

My code to do this translation looks like so:

```prolog
atom_json(true, true) :- !.
atom_json(false, false) :- !.
atom_json(null, null) :- !. 
atom_json(Number, Number) :- number(Number), !.
atom_json(Atom, String) :- atom_string(Atom, String).
 
jsonify(AtomList, JsonArray) :-
    maplist(atom_json, AtomList, JsonArray).
 
json_terms(Json, Terms) :-
    ground(Terms), !,
    maplist(=.., Terms, List),
    maplist(jsonify, List, Json).

json_terms(Json, Terms) :-
    maplist(jsonify, List, Json),
    maplist(=.., Terms, List).

```

I guess if need to convert Prolog rules to Json for some reason, something more complex would be needed. But this seems the easiest way to convert Prolog _facts_ to something other languages can read and write.

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [March 17, 2021, 11:13am UTC](https://swi-prolog.discourse.group/t/simple-json-convention-for-prolog/3724/2 "2021-03-17T11:13:53Z")

</div>

Can you discuss briefly how this compares to [library(http/json)](https://www.swi-prolog.org/pldoc/man?section=json) and [library(http/json\_convert)](https://www.swi-prolog.org/pldoc/man?section=jsonconvert)? Obviously your code attempts to be even more general I guess?

---

<div class="post-metadata">

**Author:** ![joeblog](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/joeblog/32/122_2.png) [@joeblog](https://swi-prolog.discourse.group/u/joeblog)\
**Post date:** [March 17, 2021, 12:54pm UTC](https://swi-prolog.discourse.group/t/simple-json-convention-for-prolog/3724/3 "2021-03-17T12:54:40Z")

</div>

The provided Json in SWI Prolog seems very tied to dicts as in making Json objects `{"key1": "value1", "key2": "value2", ...}` accessible using nearly the same syntax in SWI Prolog.

In my case, I want to use Json as a lingua franca between Json parsers in other programming languages, using what Json calls arrays and Prolog lists, and these don’t seem to be supported by **prolog\_to\_json** (:Term, -JSONObject) and **json\_to\_prolog** (+JSON, -Term) as far as I understand them.
