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.

docs-source (opens in a new tab)

TraitValue
Module typeutil
ImplementationPython
Parallelismsequential

Procedures

load_from_path(path)

Input:

  • path: string ➡ Path to the JSON that is being loaded.

Output:

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

Usage:

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

load_from_url(url)

Input:

  • url: string ➡ URL to the JSON that is being loaded.

Output:

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

Usage:

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

Example - Loading JSON from path

Input file

For example, let the input path be "load-data/data.json". There we can find data.json:

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

Running command

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            |
+------------------+-------------------+

Example - Loading JSON from URL

Input file

For example, let the input URL be "https://download.memgraph.com/asset/mage/data.json". There we can find data.json:

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

Running command

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              |
+------------------+-------------------+