[HELP][LUA]How to transfer a Lua table to JS?

I wrote a SQL query and the result I get is a table, when I’m trying to send it to JS I get a NULL variable.
How can I move it as an object or at least a matrix?
Thanks :slight_smile:


// Encode table as JSON string
local object = JSON.encode(yourTable)


// Decode json string as object
const obj = JSON.parse(object)

that way I can send it with SendNUIMessage?

SendNUIMessage is used to send Data between your code and user interface. As it’s explained in the documentation, SendNUIMessage attempt to receive a json string.

const retval: boolean = 
		jsonString: string

so obviously if you return your table without any alteration it’s can’t work.
I think I remember that JSON.encode() if not, you should use JSON.stringify() instead.

So an useless working code should be:

RegisterNUICallback('NUIDataTransfer', function(data)
  SetNuiFocus(true, true)
  local coord = {x=0, y=0, z=0, h=0}
  type = "locationVar",
-- Test with and without JSON ateration
  loc = JSON.encode(coord)


window.addEventListener('message', function(event) {
  if (event.data.type == "locationVar") console.log(event.data)