I’m creating an API using NodeJS and Express and can create my JSON response after querying my database. I want to minimize the size of the response and only output the values and not the keys. Here is the response I am creating.
const api = (req, res) => { pool.query(query, (err, response) => { if (err){ } res.status(200).json(response.rows); })
The response from the query is a simple table (I’m omitting columns to simplify).
Time Values 1 50 2 40 3 70 4 10
The actual JSON response is:
[{"time":1,"Values":"50"},{"time":2,"Values":"40"},{"time":3,"Values":"70"},{"time":4,"Values":"10"}]
My target output is:
[[1,50],[2,40],[3,70],[4,10]]
Is there a simple option to change with this line?
res.status(200).json(response.rows);
Or do I need to manipulate the response form the query to remove the column headers? I’m using Postgres and not sure how I would do that?
Answer
response.rows.map( row => { return [row.Time, +row.Values]; });