jallmer
jallmer

Reputation: 629

How to create a parameter on a node with the name of the parameter being a value from the file being imported with Cypher/ APOC?

I am new to neo4j and am trying to import some json formatted data. I got the first steps of reading all json files and turning some data into nodes and edges. How to create a parameter of a node on the fly, I could not figure out. SET j[issn.type] = issn.value should create a new parameter on j with the name of the value found in the json data and give it the value issn.value. The latter should be fine, but j[issn.type] does not seem to work.

How do I achieve this?

Thanks

Full Query

call apoc.load.directory("*.json") yield value as files unwind files as file 
CALL apoc.load.json(file) YIELD value as object 
UNWIND object.items AS entry 
MERGE (p:Publisher {name: entry.publisher}) 
MERGE (j:Journal {name: entry.`container-title`}) 
ON CREATE SET j.created = timestamp()
FOREACH (issn IN entry.`issn-type` | 
  SET j[issn.type] = issn.value
)
MERGE (p)-[r:publishes]->(j) 
ON CREATE SET r.created = timestamp()
RETURN p

Upvotes: 0

Views: 130

Answers (1)

Charchit Kapoor
Charchit Kapoor

Reputation: 9284

The syntax SET j[issn.type] = issn.value to set the properties in Cypher, is not yet supported. To achieve this use apoc.create.setProperty function, something like this:

call apoc.load.directory("*.json") yield value as files 
unwind files as file 
CALL apoc.load.json(file) YIELD value as object 
UNWIND object.items AS entry 
MERGE (p:Publisher {name: entry.publisher}) 
MERGE (j:Journal {name: entry.`container-title`}) 
ON CREATE SET j.created = timestamp()
WITH p, j, entry
UNWIND entry.`issn-type` AS issn 
CALL apoc.create.setProperty(j, issn.type, issn.value)
YIELD node
MERGE (p)-[r:publishes]->(node) 
ON CREATE SET r.created = timestamp()
RETURN p

Here's the documentation link. I am assuming in the query that entry.issn_type is some sort of list, that's why I am unwinding it, because we can't call apoc functions from within a FOREACH loop.

Upvotes: 1

Related Questions