[pshaiCmd] Table to JSON

0 21 Views No Comments 3 weeks ago
  • Converts InputTable object (or a regular array) to JSON data string.

    HaasScript Code
    DefineCommand('TableToJson', 'Converts InputTable to JSON data string')
    
    local inputTable = DefineParameter(ListDynamicType, 'inputTable', 'Table object to be converted', true, {}, 'InputTable')
    local tableName = DefineParameter(StringType, 'tableName', 'Name of the table', false, 'table', 'Text, Input, SessionGet, Load')
    
    local ret = '{"'.. tableName .. '":['
    
    
    for i=1, #inputTable do
        ret = ret .. '['
    
        for j=1, #inputTable[i] do
            ret = ret .. '"' .. inputTable[i][j] .. '"'
            if j < #inputTable[i] then ret = ret .. ',' end
        end
    
        ret = ret .. ']'
        if i < #inputTable then ret = ret .. ',' end
    end
    
    ret = ret .. ']}'
    
    DefineOutput(StringType, ret, 'The JSON data string', 'ParseJson')
Login or Register to Comment

Unlock your crypto trading potential

Create a free account and enjoy everything we have to offer.

Join for Free