My API needs to provide the following data in its response.
{ users: 'All users are as follows: [{id: 1}, {id: 2}]'}
The response should be a JSON object with one key value being a JSON array. However, the JSON array is converted into a string because it needs to be concatenated with another string. My current code looks like this:
const array = [{id: 1}, {id:2}]
const string = 'All users are as follows: ' + JSON.stringify(array)
res.send({users: string})
I am using Express for my API. When I view the response in Postman, extra backslashes are added to the string. Strangely, when I use console.log({a: string}) locally, I do not see any of those slashes. The response appears like this:
{users: "[{\"id\":1}, {\"id\":2}]"}