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

135
Vistas
How to send json result as a Dictionary instead of Array in expressjs

I want to send response as a dictionary like this:

{
    "id": 5928101,
    "category": "animal welfare",
    "organizer": "Adam",
    "title": "Cat Cabaret",
    "description": "Yay felines!",
    "location": "Meow Town",
    "date": "2019-01-03T21:54:00.000Z",
    "time": "2:00",
}

But I am using bellow code, which results an array

var ress = JSON.stringify(sqlResults)
console.log('response json:' + ress)
res.send(ress)

The resulted array has brackets [] like this:

[
    {
        "id": 5928101,
        "category": "animal welfare",
        "organizer": "Adam",
        "title": "Cat Cabaret",
        "description": "Yay felines!",
        "location": "Meow Town",
        "date": "2019-01-03T21:54:00.000Z",
        "time": "2:00",
    }
]

How can I send result without a third bracket?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you return the first element of your sqlResults array you should get the result you wish.

You should also be able to use res.json(dict) to send your result to the client.

sqlResults = [
    {
        "id": 5928101,
        "category": "animal welfare",
        "organizer": "Adam",
        "title": "Cat Cabaret",
        "description": "Yay felines!",
        "location": "Meow Town",
        "date": "2019-01-03T21:54:00.000Z",
        "time": "2:00",
    }
];

const dict = sqlResults[0];
var ress = JSON.stringify(dict, null, 2)
console.log('response json:' + ress)

// You can then send using res.send(ress);
// Or simply res.json(dict);

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