Skip to content

Commit

Permalink
Add JSON export docs
Browse files Browse the repository at this point in the history
  • Loading branch information
prrao87 committed Aug 20, 2024
1 parent cce01a1 commit 0199eb1
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/content/docs/export/json.md
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.

0 comments on commit 0199eb1

Please sign in to comment.