# Can json\_write\_dict/2 write an array of dicts?

**URL:** <https://swi-prolog.discourse.group/t/can-json-write-dict-2-write-an-array-of-dicts/6182>\
**Category:** General\
**Created:** [February 5, 2023, 12:06pm UTC](https://swi-prolog.discourse.group/t/can-json-write-dict-2-write-an-array-of-dicts/6182 "2023-02-05T12:06:00Z")\
**Posts on this page:** 2\
**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:** [February 5, 2023, 12:06pm UTC](https://swi-prolog.discourse.group/t/can-json-write-dict-2-write-an-array-of-dicts/6182/1 "2023-02-05T12:06:00Z")

</div>

Needed to create the file style.json ([ref](https://swi-prolog.discourse.group/t/wiki-discussion-generating-cytoscape-js-graphs-with-swi-prolog/2816/6))

File: style.json

```json
[
    {
        "selector": "node",
        "style": {
            "label": "data(id)"
        }
    },
    {
        "selector": "edge",
        "style": {
            "curve-style": "haystack"
        }
    }
]

```

and wanted to use json\_write\_dict/2.

The signature of json\_write\_dict/2 is `json_write_dict(+Stream, +Dict) is det` which lead me to believe that it could only write a single dict.

In working on style.json went the other direction to convert the JSON back to a SWI-Prolog dictionary and it worked. Then converted that back into JSON and it worked.

```prolog
?- example_01:style_dict(Dicts),json:json_write_dict(current_output,Dicts).
[
  {"selector":"node", "style": {"label":"data(id)"}},
  {"selector":"edge", "style": {"curve-style":"haystack"}}
]
Dicts = ['$VAR'('_'){selector:node, style:'$VAR'('_'){label:'data(id)'}}, '$VAR'('_'){selector:edge, style:'$VAR'('_'){'curve-style':haystack}}].

```

Along the way noticed the it was not a single SWI-Prolog dictionary but was an array of SWI-Prolog dictionaries.

Looking at the documentation more closely noticed

> Write a JSON term, represented using dicts. This is the same as [json\_write/3](https://www.swi-prolog.org/pldoc/doc_for?object=json%3Ajson_write/3), but assuming the default representation of JSON objects as dicts.

What a difference a single `s` makes.

So the question is:

Is using json\_write\_dict/2 with an array of dicts a feature or a bug? I take it that it is a feature but would hate to start using it this way only to learn it is a bug.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [February 5, 2023, 2:03pm UTC](https://swi-prolog.discourse.group/t/can-json-write-dict-2-write-an-array-of-dicts/6182/2 "2023-02-05T14:03:10Z")

</div>

> [@EricGT](#):
>
> Is using [json\_write\_dict/2](https://www.swi-prolog.org/pldoc/doc_for?object=json_write_dict/2) with an array of dicts a feature or a bug? I take it that it is a feature but would hate to start using it this way only to learn it is a bug.

The +Dict is a bit misleading. It takes any JSON value in Prolog notation, so also strings, boolean, etc. A Prolog list is mapped to a JSON array.
