Convert sql query output from JSON to indexed array

newb question, apologies if this is covered elsewhere

my sql query returns a table with categories from 1 to 8 in the category field and a second corresponding row with expenses. (in the screenshot there is just one row but there could be up to 8)

I would like to (in another script) be able to utilize something like table_output[1]['expense'] and get 225. however I can only manage json format

image

things tried:

  • data.flatMap(Object.values)
  • formatDataAsArray(data)

Suggestions?

Hey @lee_walter!

Maybe something like this could work as a transformer on that query?

const categorizedData = {};
data.forEach(row => {
categorizedData[row.category] = {expense: row.expense};
});
return categorizedData;

Hi Kabirdas,
I like where you are going . And your answer connected some dots for me about the power of transformers.

However, I am getting an error
"Could not evaluate transformer in Error: data.forEach is not a function"

I tried as per https://stackoverflow.com/questions/53275405/typeerror-data-foreach-is-not-a-function to first do JSON.parse(data). This lead me to a new error "Unexpected token o in JSON at position 1

if it helps here is a screenshot of what the untransformed data looks like
image

Thanks for your help!

Ah! You'll want to first do formatDataAsArray then, try this?

const categorizedData = {};
formatDataAsArray(data).forEach(row => {
categorizedData[row.category] = {expense: row.expense};
});
return categorizedData;
1 Like

Thanks, that worked :heavy_heart_exclamation: