CSV data
Convert CSV to JSON
There are many tools available to convert CSV to JSON. You can import large data sets to Dgraph using Dgraph Live Loader or Dgraph Bulk Loader. In these examples, the csv2json
tool is used, and the data is imported using the Mutate tab in Ratel.
Before you begin
- Install
csv2json
conversion tool. - Install
jq
a lightweight and flexible command-line JSON processor. - Connect the Dgraph instance to Ratel for queries, mutations and visualizations.
Example 1
-
Create a
names.csv
file with these details:Name,URL Dgraph,https://github.com/dgraph-io/dgraph Badger,https://github.com/dgraph-io/badger
-
Change to the directory that contains the
names.csv
file and convert it tonames.json
:$ csv2json names.csv --out names.json
-
To prettify a JSON file, use the jq ‘.’ command:
$ cat names.json | jq '.'
The output is similar to:
[ { "Name": "Dgraph", "URL": "https://github.com/dgraph-io/dgraph" }, { "Name": "Badger", "URL": "https://github.com/dgraph-io/badger" } ]
This JSON file follows the JSON Mutation Format, it can be loaded into Dgraph using Dgraph Live Loader , Dgraph Bulk Loader or the programmatic clients.
-
To load the data to Ratel and HTTP clients. The JSON data has to be stored within the
"set"
key. You can usejq
to transform the JSON into the correct format:$ cat names.json | jq '{ set: . }'
An output simialr to this appears:
{ "set": [ { "Name": "Dgraph", "URL": "https://github.com/dgraph-io/dgraph" }, { "Name": "Badger", "URL": "https://github.com/dgraph-io/badger" } ] }
-
Paste the output in the Mutate tab of Console in Ratel.
-
Click Run to import data.
-
To view the imported data paste the following in the Query tab and click Run:
{ names(func: has(URL)) { Name } }
Example 2
-
Create a
connects.csv
file that’s connecting nodes together. Theconnects
field should be of theuid
type.uid,connects _:a,_:b _:a,_:c _:c,_:d _:d,_:a
-
To get the correct JSON format, you can convert the CSV into JSON and use
jq
to transform it in the correct format where theconnects
edge is a nodeuid
. This JSON file can be loaded into Dgraph using the programmatic clients.$ csv2json connects.csv | jq '[ .[] | { uid: .uid, connects: { uid: .connects } } ]'
The output is similar to:
[ { "uid": "_:a", "connects": { "uid": "_:b" } }, { "uid": "_:a", "connects": { "uid": "_:c" } }, { "uid": "_:c", "connects": { "uid": "_:d" } }, { "uid": "_:d", "connects": { "uid": "_:a" } } ]
-
To get an output of the mutation format accepted in Ratel UI and HTTP clients:
$ csv2json connects.csv | jq '{ set: [ .[] | {uid: .uid, connects: { uid: .connects } } ] }'
The output is similar to:
{ "set": [ { "uid": "_:a", "connects": { "uid": "_:b" } }, { "uid": "_:a", "connects": { "uid": "_:c" } }, { "uid": "_:c", "connects": { "uid": "_:d" } }, { "uid": "_:d", "connects": { "uid": "_:a" } } ] }
- Paste the output in the Mutate tab of Console in Ratel, and click Run to import data.