Apoc.export.json best way to generate valid json separating nodes and relationships

I'm currently trying to export data from an existing knowledge-base so that I can perform some concept normalization and merging of other json data. As it stands now I want to get the whole database into a json file. I do that with:

CALL apoc.export.json.all("temp/XXXX_db.json")

However, this generates an invalid json document. Each row is a valid json object, but the file itself is not and takes the following type of format:

{"type":"node", "id":"...", "labels":["..."],"properties":{...}}
{"type":"node", "id":"...", "labels":["..."],"properties":{...}}
{"type":"node", "id":"...", "labels":["..."],"properties":{...}}
{"type":"node", "id":"...", "labels":["..."],"properties":{...}}
{"id":"0","type":"relationship","label":"...","start":{"id":"...","labels":["..."]},"end":{"id":"...","labels":["..."]}}
{"id":"1","type":"relationship","label":"...","start":{"id":"...","labels":["..."]},"end":{"id":"...","labels":["..."]}}

Is there an easier way to automatically write the json out into a format like:

{"node":[{"labels":["..."],
          "properties":{...}},
          {...}],
"relationship": [{"id":"1",
                  "label":"...",
                  "start":{"id":"...","labels":["..."]},
                  "end":{"id":"...","labels":["..."]}
                  },
                  {...}]
}

Where the nodes and relationships are grouped in separate entries in the json?

I could manually do this, or use an external language to read the json line by line and then write out a new, valid, json file, but I have a hunch that someone might be able to suggest an easier way to do it by using Cypher.

1 Like

I have the same issue, quite weird that the output JSON isn't a valid JSON, took me a while to figure it out, I really wasn't expecting it...

1 Like

I wonder if anyone is paying attention to these posts. A valid JSON file should be produced.

Hi guys,
the apoc.export.json at this very moment exports valid Json lines:
https://jsonlines.org/
It's a format supported by various big data frameworks such as Apache Spark

Btw we merged a new feature that will be available in the next release:

That will add support for single json formats.

If you want you can test it by compiling the project from the source

1 Like