Gremlin queries containing Nested Objects & Apostrophes

I'm getting into GraphDB and I've just started making use of Gremlin alongside NeptuneDB to build a demo application. I've been running into an issue for a while now, first is: - I'm sending data to the Neptune DB that contains Nested Objects / Array of Nested Objects, what's the better way to run a Gremlin query that creates a property with the objects therein? What I've had to do is write an algorithm that checks if i'm sending objects, then I stringify the object before sending, Then whenever I'm receiving back the data, I parse the values from the keys
function generateGremlinPostArticleQuery(data) {
let query = `g.addV('article')`;
for (const key in data) {
if (data.hasOwnProperty(key)) {
if (typeof data[key] === 'object') {
query += `.property('${key}', '${JSON.stringify(data[key])}')`;
continue;
}
query += `.property('${key}', '${data[key]}')`;
}
}
return query;
}
function generateGremlinPostArticleQuery(data) {
let query = `g.addV('article')`;
for (const key in data) {
if (data.hasOwnProperty(key)) {
if (typeof data[key] === 'object') {
query += `.property('${key}', '${JSON.stringify(data[key])}')`;
continue;
}
query += `.property('${key}', '${data[key]}')`;
}
}
return query;
}
Secondly: How can I escape the apostrophe in my strings " ' "? I know of using the forward slashes \'...but this doesn't work accurately when I have nested JSON. Or different data structures across my request body P:S - I'm a beginner with GraphDB and would appreciate references to using Gremlin/NeptuneDB as well as there aren't enough documentation out there
2 Replies
spmallette
spmallette16mo ago
Neptune (as is the case for many graphs) do not support storage of complex objects. If you must store them then converting to JSON (or binary for some graphs) is probably the only way you can do that. The limitation of course to storing complex objects this way is that their contents aren't easily searchable with Gremlin. as for the apostrophe, you would need to double escape them essentially - here i do it with double quotes, but you could do the same with apostrophe: "{\"thing\": {\"a\": \"x\\\"\"}}"
jideabdqudus
jideabdqudus16mo ago
Thank you so much ! @spmallette
Want results from more Discord servers?
Add your server