Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
Formatting multiple query returns as a JSON object

I am using JavaScript and neo4j-driver to save a node and some relations to my graph. Everything works as expected, but I would like to read the results of my write and output it in a specific format afterwards, which I am having trouble with. For now I have only managed to return all of the variables inside my query, but I would like the output to be in this specific format:

{
  "name": p.name
  "colors: [
     {
        hex: c.hex
     }
     {
        hex: c.hex
     }
   ]
}

This is my code so far, any help is appreciated:

export const savePallete = async (palette: Palette) => {
  if (!!palette.name === false) {
    throw Error("Palette name required!");
  }

  const writeQuery = `MERGE (p:Palette {name: $name})
   ON CREATE SET p.name = $name
   FOREACH (color IN $colors |
   MERGE (c:Color {hex: color.hex})
   ON CREATE SET c.hex = color.hex
   CREATE (p)-[i:INCLUDES]->(c))
   `;
  await session.writeTransaction((tx) =>
    tx.run(writeQuery, { name: palette.name, colors: palette.colors })
  );

  const readQuery = `
    MATCH (p:Palette)
    WHERE p.name = $name
    MATCH (p)-[:INCLUDES]->(c:Color)
    RETURN *
  `;
  const readResult = await session.readTransaction((tx) =>
    tx.run(readQuery, { name: palette.name })
  );
  return readResult;
};

As an example, if I call savePallete with this object as the argument, I would want to to get back the same result:

{
    "name": "First test palette",
    "colors": [
        {
            "hex": "#123123"
        },
        {
            "hex": "#ffffff"
        },
        {
            "hex": "#000000"
        }
    ]
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

One way to do it is to replace your readQuery with (last 2 lines are different):

    MATCH (p:Palette)
    WHERE p.name = $name
    MATCH (p)-[:INCLUDES]->(c:Color)
    WITH COLLECT({hex: c.hex}) AS colors, p
    RETURN {name: p.name, colors: colors}

This will collect the colors` hex as a JSON and will return a JSON format.

When using sample data:

MERGE (a:Palette {name: "First test palette"})
MERGE (b:Color {hex: "#123123"})
MERGE (c:Color {hex: "#ffffff"})
MERGE (d:Color {hex: "#000000"})
MERGE (a)-[:INCLUDES]-(b)
MERGE (a)-[:INCLUDES]-(c) 
MERGE (a)-[:INCLUDES]-(d) 

it returns your expected results

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda