json_util

A module for loading JSON from a local file or remote address. If the JSON that is being loaded is an array, then this module loads it as a stream of values, and if it is a map, the module loads it as a single value.

TraitValue
Module typeutil
ImplementationPython
Parallelismsequential

Procedures

load_from_path()

The procedure loads data from a local JSON file.

Input:

  • subgraph: Graph (OPTIONAL) ➡ A specific subgraph, which is an object of type Graph returned by the project() function, on which the algorithm is run. If subgraph is not specified, the algorithm is computed on the entire graph by default.
  • path: string ➡ Path to the JSON that needs to be loaded.

Output:

  • objects: List[object] ➡ The list of JSON objects from the file that is being loaded.

Usage:

Use the folowing query to load data from a local JSON file:

CALL json_util.load_from_path(path) 
YIELD objects
RETURN objects;

load_from_url()

The procedure loads data from a JSON file at a specified URL.

Input:

  • subgraph: Graph (OPTIONAL) ➡ A specific subgraph, which is an object of type Graph returned by the project() function, on which the algorithm is run. If subgraph is not specified, the algorithm is computed on the entire graph by default.
  • url: string ➡ URL to the JSON that needs to be loaded.

Output:

  • objects: List[object] ➡ A list of JSON objects from the file that is being loaded.

Usage:

Use the following query to load data from a JSON file at a specified URL:

CALL json_util.load_from_url(url) 
YIELD objects
RETURN objects;

Examples

Load JSON from path

Input file

The data.json file is located in the load-data directory with the following content:

{
	"first_name": "Jessica",
	"last_name": "Rabbit",
	"pets": [
		"dog",
		"cat",
		"bird"
	]
}

Import data

Run the query to load the data from the file:

CALL json_util.load_from_path("load-data/data.json") 
YIELD objects
UNWIND objects AS o
RETURN o.first_name AS name, o.last_name AS surname;

Results:

+------------------+-------------------+
| name             | surname           |
+------------------+-------------------+
| Jessica          | Rabbit            |
+------------------+-------------------+

Load JSON from URL

Input file

The data.json file is located at "https://download.memgraph.com/asset/mage/data.json" with the following content:

{
	"first_name": "James",
	"last_name": "Bond",
	"pets": [
		"dog",
		"cat",
		"fish"
	]
}

Import data

Run the query to load the data from the file at the URL:

CALL json_util.load_from_url("https://download.memgraph.com/asset/mage/data.json") 
YIELD objects
UNWIND objects AS o
RETURN o.first_name AS name, o.last_name AS surname;

Results:

+------------------+-------------------+
| name             | surname           |
+------------------+-------------------+
| James            | Bond              |
+------------------+-------------------+