I'm not sure that I'm using the correct terminology, so feel free to correct it when necessary. I (think) I have JSON objects that I'm attempting to convert into an array of elements.
I'm using Neography in Ruby to query a Neo4j database, and when I receive the results back, I need them to look like this:
["apple","orange","pear"]
However, they look like this:
[["apple"],["orange"],["pear"]]
The Ruby I'm using to create this is:
cypher = "MATCH (n:Person) WHERE n.name =~ '(?i).*#{term}.*' RETURN n.name"
results = neo.execute_query(cypher)["data"].to_json
puts results
results
I'd read here (How to Remove Square bracket from JSON) to try parsing the JSON, and getting the first element.
cypher = "MATCH (n:Person) WHERE n.name =~ '(?i).*#{term}.*' RETURN n.name"
results = neo.execute_query(cypher)["data"].to_json
results = JSON.parse(results)
puts results
results.to_s # deals with Sinatra not being able to display a hash?
But gotten the same double-bracket results.
I'd suggest looking into the Neo4j.rb project (the
neo4j-core
andneo4j
gems). Full disclosure: I'm one of the maintainers ;). Withneo4j-core
you might do:You should be using parameters in general, though, to prevent injection attacks:
You can also use the
Query
API which will make params for you and also convert Ruby regular expressions to Cypher syntax:If you use
ActiveNode
from theneo4j
gem it's even simpler:You could even make a search method on the
Person
model to do that for you: