Database.raw() how to serialize result to JSON

#1

I am running a raw query sql query

const result = await database.raw("select * from sometable")

I need to return the result back to my vuejs client as CSV data.

I have been trying to use the json2csv library https://www.npmjs.com/package/json2csv but it seems to be struggling with the serialized result created by database.raw()

How do I turn that database.raw() serialized result to a plain old JSON array of simple objects?

1 Like

#2

I solved it.

The serialization returns an array of two objects.

first object is your data
second object is a description of the data

So simply using result[0] gets you your data that in my case, json2csv library can work with.

1 Like