Extracting Objects Recursively with Jq
JSON data is also a valid <i>Prolog</i> term, and the declarative programming language Prolog is ideally suited for handling tree-shaped data.<p>Using for example Scryer Prolog, we can conveniently <i>relate</i> the data to a flat list of items with Prolog&#x27;s built-in grammar mechanism, definite clause grammars (DCGs):<p><pre><code>    flat_json(JSON) --&gt;
            { JSON = {A,B,C,D,E,F,_:Cs} },
            [{A,B,C,D,E,F}],
            flat_items(Cs).

    flat_items([]) --&gt; [].
    flat_items([I|Is]) --&gt;
            { I = {(A,B,C,D,E,_:Cs)} },
            [{A,B,C,D,E}],
            flat_items(Cs),
            flat_items(Is).
</code></pre>
Sample query, using the example JSON data from the article:<p><pre><code>     ?- JSON = {
        &quot;id&quot;: 27941108,
        &quot;created_at&quot;: &quot;2021-07-24T14:15:05.000Z&quot;,
        &quot;type&quot;: &quot;story&quot;,
        &quot;author&quot;: &quot;edward&quot;,
        &quot;title&quot;: &quot;Fun with Unix domain sockets&quot;,
        &quot;url&quot;: &quot;https:&#x2F;&#x2F;simonwillison.net&#x2F;2021&#x2F;Jul&#x2F;13&#x2F;unix-domain-sockets&#x2F;&quot;,
        &quot;children&quot;: [
            {
                &quot;id&quot;: 27942287,
                &quot;created_at&quot;: &quot;2021-07-24T16:31:18.000Z&quot;,
                &quot;type&quot;: &quot;comment&quot;,
                &quot;author&quot;: &quot;DesiLurker&quot;,
                &quot;text&quot;: &quot;&lt;p&gt;one lesser known...&quot;,
                &quot;children&quot;: []
            },
            {
                &quot;id&quot;: 27944615,
                &quot;created_at&quot;: &quot;2021-07-24T21:26:33.000Z&quot;,
                &quot;type&quot;: &quot;comment&quot;,
                &quot;author&quot;: &quot;galaxyLogic&quot;,
                &quot;text&quot;: &quot;&lt;p&gt;I read this from Wikipedia...&quot;,
                &quot;children&quot;: [
                    {
                        &quot;id&quot;: 27944746,
                        &quot;created_at&quot;: &quot;2021-07-24T21:49:07.000Z&quot;,
                        &quot;type&quot;: &quot;comment&quot;,
                        &quot;author&quot;: &quot;hughrr&quot;,
                        &quot;text&quot;: &quot;&lt;p&gt;Yes although I ...&quot;,
                        &quot;children&quot;: []
                    }
                ]
            }
        ]
    },
       phrase(flat_json(JSON), Cs),
       maplist(portray_clause, Cs).

</code></pre>
yielding the flat list of entries, as desired:<p><pre><code>    [{(&quot;id&quot;:27941108,&quot;created_at&quot;:&quot;2021-07-24T14:15:05.000Z&quot;,&quot;type&quot;:&quot;story&quot;,&quot;author&quot;:&quot;edward&quot;,...)},
     {(&quot;id&quot;:27942287,&quot;created_at&quot;:&quot;2021-07-24T16:31:18.000Z&quot;,&quot;type&quot;:&quot;comment&quot;,...)},
     {(&quot;id&quot;:27944615,&quot;created_at&quot;:&quot;2021-07-24T21:26:33.000Z&quot;,&quot;type&quot;:&quot;comment&quot;,...)},
     {(&quot;id&quot;:27944746,&quot;created_at&quot;:&quot;2021-07-24T21:49:07.000Z&quot;,&quot;type&quot;:&quot;comment&quot;,...)}]</code></pre>