cancel
Showing results for 
Search instead for 
Did you mean: 

How to pass an external variable inside the load.json path?

damisg7
Node Clone

I would like to pass an external variable to:

call apoc.load.json("file:///...")

I tried the command below (with the appropriate query run in python) but it didn't worked with an error of not finding the file.

call apoc.load.json("file:///$filename")

So how i'm going to pass the external variable to this path ?

1 ACCEPTED SOLUTION

If you are running it through driver from java or python. Try form the query with string concatenation and pass it.

x =  {'filename': 'filename.json'}
q="call apoc.load.json('file:///"+x['filename']+"')"

this is in python, check with quotes accordingly.This is the not the way you want but this will do the work or as @dana.canzano said

q = """call apoc.load.json($filepath)"""
x= {'filepath': 'file:///filename.json'}
session.run(q,x)

View solution in original post

3 REPLIES 3

dana_canzano
Neo4j
Neo4j

perhaps 2 options and tested with Neo4j 4.2.3 and cypher-shell but

@neo4j> :param f=>'file:///example.json'
@neo4j> :params
:param f => 'file:///example.json'
@neo4j> call apoc.load.json($f) yield value;

which defines parameter f to have the value file:///example.json'

or

with 'file:///example.json' as f call apoc.load.json(f) yield value return value;

I did know these options though i can't find the connection on how i would pass the last part of the path (the file.json) as an external variable. For example how i'm gonna do this:

q = """call apoc.load.json('file:///$filename')"""
x= {"filename": filename}
session.run(q,x)

Because, that way doesn't work (and it's logical because inside the path cypher can't recognize the symbol $)

If you are running it through driver from java or python. Try form the query with string concatenation and pass it.

x =  {'filename': 'filename.json'}
q="call apoc.load.json('file:///"+x['filename']+"')"

this is in python, check with quotes accordingly.This is the not the way you want but this will do the work or as @dana.canzano said

q = """call apoc.load.json($filepath)"""
x= {'filepath': 'file:///filename.json'}
session.run(q,x)
Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.