I need to encode a json object with an array from a lua table.

function encode_flow_record(type,a, b, c)
    local i = 1
    while (parent~= nil) do
         local array[i] = {f1=a,f2=b,f3=c}
         parent, a,b,c = next(a,b,c)
         i = i + 1

    local datastring = cjson.encode({flowrecord=type, ancestors=array})

When I print datastring, it shows "ancestors":{}

What am I missing?

  • Please ignore the uninitialized parent in the first iteration of the loop (code snippet is a shortened version). The question is about how to encode the array[] – anglee Mar 12 at 2:54
  • 4
    local array[i] = ... is not valid Lua code. – lhf Mar 12 at 10:32
  • 2
    You have to initialize your table outside the while statement. local array = {} – csaar Mar 12 at 12:18

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.