# Exporting json that has a structure constructed from elements matched in DB

**URL:** <https://community.neo4j.com/t/exporting-json-that-has-a-structure-constructed-from-elements-matched-in-db/16670>\
**Category:** Import / Export\
**Tags:** apoc, cypher\
**Created:** [April 4, 2020, 1:07pm UTC](https://community.neo4j.com/t/exporting-json-that-has-a-structure-constructed-from-elements-matched-in-db/16670 "2020-04-04T13:07:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![v2belleville](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/v2belleville/32/29050_2.png) [@v2belleville](https://community.neo4j.com/u/v2belleville)\
**Post date:** [April 4, 2020, 1:07pm UTC](https://community.neo4j.com/t/exporting-json-that-has-a-structure-constructed-from-elements-matched-in-db/16670/1 "2020-04-04T13:07:29Z")

</div>

I have a graph structure that looks like the one on the left and I need a json file with the structure to the right

 ![grph shema](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/0/0952278bc7e73220c14233fb1d479c7e08526646.png)

the purpose is to get a list of counties with , for each of them, the corresponding list of collectives names, with optional attached details (url by type - facebook, twitter or website and email address)

here is for example how county n°13 looks like in DB : it has 3 collectives attached. Urls are the green nodes , an email address is node with property thgType="email adress"

 ![graph 13](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/e/e065ccea1162379c478e7b34b80f35fb4ca68b39.png)

do I need to create , in the DB, the nodes and relations corresponding to the structure I want to export ?

if so, how do I do that ? i've been struggling with foreach, unwind, create, call , ... and could figure it out !

do I need apoc.convert.toJson ? cause I got this error trying to use id

```auto
Failed to invoke function `apoc.convert.toJson`: Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.neo4j.graphdb.Label$1 and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0]->org.neo4j.kernel.impl.core.NodeEntity["labels"]->java.util.ArrayList[0])

```

any hint on how to construct a new structure by picking elements in DB highly appreciated !

(tried my best to make the question clear, but please ask if it is not ... )

---

<div class="post-metadata">

**Author:** ![D\_C](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/d_c/32/19041_2.png) [@D\_C](https://community.neo4j.com/u/D_C)\
**Post date:** [July 5, 2021, 9:34pm UTC](https://community.neo4j.com/t/exporting-json-that-has-a-structure-constructed-from-elements-matched-in-db/16670/2 "2021-07-05T21:34:19Z")

</div>

I'm also looking into serialization. Did you get the `apoc` function to work in the end?  
What language are you using?

From python I think you can just do `result.dict()` but you still have to reformat the results.

Did you review these posts about using subgraph and `apoc.convert` ?

> [@Extracting subgraph into JSON format](https://community.neo4j.com/t/extracting-subgraph-into-json-format/3416/5):
>
> Okay, so you want JSON output of a list of nodes (all properties + label, assuming only a single label per node in your model), and a list of relationships (all properties + type, + start and end nodes but only the label and key for each node. This query should do the trick. MATCH (n) WHERE n.user\_id='0000001' CALL apoc.path.subgraphAll(n, {maxLevel:1}) YIELD nodes, relationships WITH [node in nodes | node {.\*, label:labels(node)[0]}] as nodes, [rel in relationships | rel {.\*, fromNode:…

---

<div class="post-metadata">

**Author:** ![v2belleville](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/v2belleville/32/29050_2.png) [@v2belleville](https://community.neo4j.com/u/v2belleville)\
**Post date:** [August 5, 2021, 8:58am UTC](https://community.neo4j.com/t/exporting-json-that-has-a-structure-constructed-from-elements-matched-in-db/16670/3 "2021-08-05T08:58:31Z")

</div>

what I was missing at the time is the use of `collect()` which basically is all I needed

I've presented how it's done here [https://youtu.be/-mtFqb2U6Sc?t=2450](https://youtu.be/-mtFqb2U6Sc?t=2450)

the details are presented here [http://www.e-tissage.net/nodes2020-using-cypher-to-extend-a-graph-based-documentation/](http://www.e-tissage.net/nodes2020-using-cypher-to-extend-a-graph-based-documentation/)
