JSON.parse() is returning [Object object]

I have a function in a backend code file that successfully retrieves and returns data from an external database:

export async function getData() {
/*
DB authorization code here
*/

 let result = await db.query.execute({
        sql: `select * from ${'dataset name'}.${'table name'},
        params: { id: 2 }
    })
   
 result = JSON.stringify(result, null, ' ');
 return result;
}

I then call the function getData() in my frontend page code to get the string of data, and then output the response to the console:

import { getData } from 'backend/data';

$w.onReady(function () {
    getData().then(response => {
        console.log("Response: " + response);
    })
})

The output to the console is, as expected, a string of data (simplified for example’s sake):

Response:
[
  {
  "name": "John",
  "id": "1",
  "value": "100",
 },
 {
  "name": "Tim",
  "id": "2",
  "value": "200",
 },
 {
  "name": "Jim",
  "id": "3",
  "value": "300",
 }
]

However, when using JSON.parse on this string, I’m getting an unexpected result:

import { getData } from 'backend/data';

$w.onReady(function () {
    getData().then(response => {
        let response_obj = JSON.parse(response);
        console.log("Response obj: " + response_obj);
    })
})

This is what logs to the console:

Response obj: [object Object],[object Object],[object Object]

What is going wrong in the code above where I’m getting “[Object object]” for each item in the database, rather than a Javascript object containing all of the data shown above?

Thank you in advance for your help!

Nothing, it behaves just as expected: you had a JSON object, you turned it into a string using stringify, you hand it over and then you turn it into an JSON object again using parse. If you then console.log it, you WILL get Object, because… it is.
In short: what’s the problem?
And something else: why go thru all the trouble of converting an object into a string and than back again into an object? Why not hand over the object without conversion?

Hey Giri,

Thank you so much for taking the time to respond to my question.

Simply put, how can I get this data in the form where I can access it just by response[0].name, response[1] .value, etc.?

Each item comes in from the database as [Object object], and only after JSON.stringify am I able to see the actual values. But when I have the data as a string, I am not able to access anything by referencing the object’s keys. Do you have any suggestions that may help?

Hopefully this makes a little more sense.

Thank you again!

Best,
Kevin

Well, more or less exactly as you proposed. But do me a favor. You wrote earlier:
“The output to the console is, as expected, a string of data (simplified for example’s sake):”

Could you dump the non-simplified output?