Home > Software design >  How to return JSON with array instead of wrapped array
How to return JSON with array instead of wrapped array

Time:09-17

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});

});

It returns:

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

I want to have

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

CodePudding user response:

Just:

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

you should remove the {} from {objects}

  • Related