cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Change key names inferred in convert.toTree

yarden
Node Link

Hello There,

I'm calling apoc.convert.toTree and wondered whether it's possible to change the key names that come from the relationship names.

For example, I have something along these lines:

{
 name: "repo1",
 description: "first repo",
 job: "host commands",
 hosts: [
   {...}
 ]
}

I want to change the key 'hosts' to 'commands', 'hosts' is the name of the relationship but in a structured json file, 'commands' will do a better job. I named it hosts as a verb: It hosts the commands.

Inside each command (the array named 'hosts') I have another array with a name that comes from another relationship that I wish to change as well.

In the db these are not arrays, these are nodes with subnodes (with subnodes of their own).

I'm matching the path, then collecting it, then feeding it to "call apoc.convert.toTree" and returning the yielded value.

1 ACCEPTED SOLUTION

yarden
Node Link

Found a way around it. I hope this helps people in the future. Even though it's Golang in my case, other languages use tags as well.

Fields in Golang can be tagged so when decoded and encoded the decoder will know what name to use (this, of course, can differ from the field name). They can be tagged for several causes. meaning, a specific field can have different tags for JSON, XML, etc.

In my case I used 2 tags, json and "mapstructure" which is a Golang package. I decode from neo4j using mapstructure, and then send it to my frontend using the json tag. This is a Golang solution, but it solved my problem so I'm marking it as a solution.

I'm still curious about whether there is a solution for that using apoc.

View solution in original post

2 REPLIES 2

yarden
Node Link

I'm still facing this problem. Any possibility that anyone has an answer to this?

Summing and rephrasing:
I'm converting a whole subtree into a struct (Golang). After finding the path(s) I'm using apoc.convert.toTree and then apoc.convert.toJson. Then I'm decoding it to a struct.

The issue: These methods create a json that uses the relationships' names but my entire system uses different names. For example. a user might be [:SUBSCRIBED_TO] a service and that's what the json will create. But my system uses a field called "subscriptions" under the user struct.

Is there a way to change these labels when apoc.convert does its work?

yarden
Node Link

Found a way around it. I hope this helps people in the future. Even though it's Golang in my case, other languages use tags as well.

Fields in Golang can be tagged so when decoded and encoded the decoder will know what name to use (this, of course, can differ from the field name). They can be tagged for several causes. meaning, a specific field can have different tags for JSON, XML, etc.

In my case I used 2 tags, json and "mapstructure" which is a Golang package. I decode from neo4j using mapstructure, and then send it to my frontend using the json tag. This is a Golang solution, but it solved my problem so I'm marking it as a solution.

I'm still curious about whether there is a solution for that using apoc.