[pshaiCmd] JSON to Table

1 245 Views No Comments 6 months ago
  • Converts a JSON object/data string to a table/array.

    HaasScript Code
    DefineCommand('JsonToTable', 'Converts JSON string/object to a table')
    
    local input = DefineParameter(DynamicType, 'input', 'JSON string or object', true, '', 'Text, Input, ParseJson')
    local jsonToTable
    
    jsonToTable = function(input, isCell)
        if isCell == nil then isCell = false end
        local ret = {}
    
        if input[1] != nil then
            for i=1, #input do
                ret[i] = jsonToTable(input[i], true)
            end
        else
            ret = input
        end
    
        local ret2 = {}
        if not isCell then
            for i=1, #ret[1] do
                ret2[i] = ret[1][i]
            end
        end
    
        return isCell and ret or ret2
    end
    
    local ret = {}
    
    if GetType(input) == UserDataDataType then
        ret = jsonToTable(input)
    elseif GetType(input) == TextDataType and #input > 0 then
        ret = jsonToTable(ParseJson(input))
    end
    
    DefineOutput(ListDynamicType, ret, 'Table containing values from JSON')
Login or Register to Comment

Unlock your crypto trading potential

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

Join for Free