Question about lua table creation

I’m having trouble adding multiple values to a table. I think it is just a syntax issue but would appreciate any suggestions.

I have the following table defined:

local nameData = {category,price}

  1. I grab the Json data (coming from a mysql database)

myData = json.decode(event.response)

myData looks like this:

{ “data”:[{“price”:125,“duration”:300,“currency”:“USD”,“category”:“Wilderness Training”},{“price”:125,“duration”:300,“currency”:“USD”,“category”:“Camping”}, etc

  1. I am trying to insert the myData into nameData. The following works but it does not insert a new row in the table and simply adds the last data set.

for i=1, #myData.data do

      if myData.data[i].category ~= nil then

        for k,v in pairs( myData) do

          nameData[i].category=myData.data[i].category

          nameData[i].price=myData.data[i].price

        end

      end

    end

The result is {category=blah,price=111} but only a single row containing the last record in the json data set.

  1. Table.insert only allows for one value. I tried the following but none work:

This adds two new rows

table.insert( nameData, myData.data[i].category)

table.insert(nameData, myData.data[i].price)

This errors out

table.insert(nameData, myData.data[i].category, myData.data[i].price)

And so does this

table.insert(nameData, {myData.data[i].category, myData.data[i].price})

I’ve tried 4 or 5 other ways but keep hitting a wall.

Any suggestions? Thanks

Solved

nameData={}

for i=1, #myData.data do

      if myData.data[i].category ~= nil then

        for k,v in pairs( myData) do

          local message{

          category =myData.data[i].category,

          price =myData.data[i].price

          }

          table.insert(nameData,message)

        end

      end

    end

 

Result is [{category=blah,price=111},{category=blahblah,price=222},etc]

 

This link helped:

http://stackoverflow.com/questions/10627548/multivalue-table-in-lua

Solved

nameData={}

for i=1, #myData.data do

      if myData.data[i].category ~= nil then

        for k,v in pairs( myData) do

          local message{

          category =myData.data[i].category,

          price =myData.data[i].price

          }

          table.insert(nameData,message)

        end

      end

    end

 

Result is [{category=blah,price=111},{category=blahblah,price=222},etc]

 

This link helped:

http://stackoverflow.com/questions/10627548/multivalue-table-in-lua