Skip to main content

Command Palette

Search for a command to run...

JSON Beautify for BrightScript

Published
1 min readView as Markdown
' JSON Beautifier for BrightScript
' By BrightScripters 
function json_beautify(in$) as string
    indentLevel = 0
    out$ = ""
    newLine$ = chr(13) + chr(10)

    for i = 0 to in$.len() - 1
        char$ = in$.mid(i,1)

        if char$ = "{" or char$ = "[" then
            indentLevel = indentLevel + 1
            out$ = out$ +  char$ + newLine$ + indentStr(indentLevel)
        else if char$ = "}" or char$ = "]" then
            indentLevel = indentLevel - 1
            out$ = out$ + newLine$ + indentStr(indentLevel) + char$
        else if char$ = ":" then
            out$ = out$ + char$ + " " 
        else if char$ = "," then
            out$ = out$ + char$ + newLine$ + indentStr(indentLevel)
        else
            out$ =  out$ + char$
        end if

    end for

    return out$
end function


function indentStr( indentLevel ) as string
    indentStr$ = ""
    for i = 0 to indentLevel - 1
        indentStr$ = indentStr$ + "    "
    end for
    return indentStr$
end function