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

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 ?

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)
1 Like