# Exporting a Hierarchy in JSON: with recursive queries

DevFeed: [Exporting a Hierarchy in JSON: with recursive queries](<https://devfeed.tech/articles/exporting-a-hierarchy-in-json-with-recursive-queries-34579.md>)

Original publisher: [Read original article](<https://tapoueh.org/blog/2018/01/exporting-a-hierarchy-in-json-with-recursive-queries/>)

Author: Dimitri Fontaine PostgreSQL Major Contributor; Author

Published: 2018-01-31T17:00:01Z

Content type: tutorial

Language: en

Sources: [Dimitri Fontaine](<https://devfeed.tech/sources/dimitri-fontaine.md>)

Topics: [SQL](<https://devfeed.tech/topics/sql.md>), [PostgreSQL](<https://devfeed.tech/topics/postgresql.md>), [export](<https://devfeed.tech/topics/export.md>), [JSON](<https://devfeed.tech/topics/json.md>), [data](<https://devfeed.tech/topics/data.md>), [JavaScript](<https://devfeed.tech/topics/javascript.md>)

Tags: [data](<https://devfeed.tech/tags/data.md>), [export](<https://devfeed.tech/tags/export.md>), [file](<https://devfeed.tech/tags/file.md>), [json](<https://devfeed.tech/tags/json.md>), [postgresql](<https://devfeed.tech/tags/postgresql.md>), [sql](<https://devfeed.tech/tags/sql.md>)

## AI overview

This tutorial explains how to use PostgreSQL and recursive SQL queries to export hierarchical relational data as a single JSON document. It models parent-child Dungeons & Dragons character classes and prepares the resulting JSON for client-side visualization with d3js.

## Source excerpt

In another article here, entitled on JSON and SQL, we saw in great details how to import a data set only available as a giant JSON file. Then we normalized the data set, so as to be able to write SQL and process our data. This approach is sometimes very useful and was a good way to learn some of the JSON functions provided by PostgreSQL. In this article, we're going to use SQL to export the data from our relational model into a JSON document. The trick that makes it complex in this example is that we have a recursive data model, with a notion of a parent row that exists in the same table as the current one. That's a nice excuse to learn more about the SQL construct WITH RECURSIVE.