-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
title: Export to JSON | ||
--- | ||
|
||
The `COPY TO` clause can export query results to a JSON file. To use this feature, you have to install the | ||
JSON extension using the instructions shown [here](/extensions/json). | ||
|
||
To demonstrate this, we will create a node table and insert some data into it. | ||
```sql | ||
CREATE NODE TABLE Person (id SERIAL, name STRING, info STRUCT(height DOUBLE, age INT64, previous_usernames STRING[]), PRIMARY KEY(id)); | ||
``` | ||
```cypher | ||
CREATE (:Person {name: "Alice", info: {height: 1.68, age: 45, previous_usernames: ["alice123", "alice_34425"]}}); | ||
CREATE (:Person {name: "Bob", info: {height: 1.81, age: 71, previous_usernames: ["the_builder", "the_minion"]}}); | ||
CREATE (:Person {name: "Gregory", info: {height: 1.73, age: 22, previous_usernames: ["gregory7"]}}); | ||
``` | ||
|
||
The following query will export the data from the `Person` node table to a JSON file. | ||
```sql | ||
COPY (MATCH (p:Person) RETURN p.*) TO 'people-output.json'; | ||
``` | ||
Result: | ||
|
||
```json | ||
[ | ||
{"p.id":0,"p.name":"Alice","p.info":{"height":1.68,"age":45,"previous_usernames":["alice123","alice_34425"]}}, | ||
{"p.id":1,"p.name":"Bob","p.info":{"height":1.81,"age":71,"previous_usernames":["the_builder","the_minion"]}}, | ||
{"p.id":2,"p.name":"Gregory","p.info":{"height":1.73,"age":22,"previous_usernames":["gregory7"]}} | ||
] | ||
``` | ||
|
||
You can use a JSON formatter to make the output more readable. |