Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return JSON with array instead of wrapped array

I am using express for my REST API and when I get objects it returns me wrapped array. I would like to have it returned as simple array.

My code looks like that:

router.get('/objects', async (req: Request, res: Response) => {
const objects = await getConnection()
    .getRepository(Object)
    .createQueryBuilder("object")
    .getMany();
return res.status(OK).json({objects});

});

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It returns:

{
 objects: [
  {
    id: "11",
    name: "Eleven"
  },
  {
    id: "12",
    name: "Twelve"
  }
 ]
}

I want to have

  {
    id: "11",
    name: "Eleven"
  },
  {
    id: "12",
    name: "Twelve"
  }

>Solution :

Just:

return res.status(OK).json(objects);

you should remove the {} from {objects}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading