canbax
canbax

Reputation: 3856

neo4j cypher convert array/list to string

One type of edges in my graph has a property called roles. It is an array/list of strings. It is like ["Bill Smoke", "Haskell Moore", "Tadeusz Kesselring", "Nurse Noakes", "Boardman Mephi", "Old Georgie"]

How can I convert this to a string? I want to join them. With JS, I can do ['asd', '1', '2'].join(''). I want a similar functionality inside cypher

Upvotes: 5

Views: 8315

Answers (3)

InverseFalcon
InverseFalcon

Reputation: 30397

APOC Procedures has support for several categories of helper functions, this one included.

When installed, you can use apoc.text.join() to get the desired result:

WITH ["Bill Smoke", "Haskell Moore", "Tadeusz Kesselring", "Nurse Noakes", "Boardman Mephi", "Old Georgie"] as list
RETURN apoc.text.join(list, ' ') as string

Upvotes: 9

Graphileon
Graphileon

Reputation: 5385

WITH ["Bill Smoke", "Haskell Moore", "Tadeusz Kesselring", "Nurse Noakes", "Boardman Mephi", "Old Georgie"] AS array,
     ';' AS separator
RETURN REDUCE(mergedString = "",item IN array | 
              mergedString
              + CASE WHEN mergedString='' THEN '' ELSE separator END
              + item) AS mergedString

Upvotes: 2

TheTeacher
TheTeacher

Reputation: 510

WITH REDUCE(mergedString = "",word IN ["Bill Smoke", "Haskell Moore", "Tadeusz Kesselring", "Nurse Noakes", "Boardman Mephi", "Old Georgie"] | mergedString+word+',') as joinedString
RETURN LEFT(joinedString,SIZE(joinedString)-1)

REDUCE is the function you are basically looking for . https://neo4j.com/docs/cypher-manual/current/functions/list/#functions-reduce

Upvotes: 5

Related Questions