Sample code for 30+ languages & platforms
PureBasic

Pretty Print JSON (Formatter, Beautifier)

See more JSON Examples

Demonstrates how to emit JSON in a pretty, human-readable format with indenting of nested arrays and objects.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    jsonStr.s = "{" + Chr(34) + "name" + Chr(34) + ": " + Chr(34) + "donut" + Chr(34) + "," + Chr(34) + "image" + Chr(34) + ":{" + Chr(34) + "fname" + Chr(34) + ": " + Chr(34) + "donut.jpg" + Chr(34) + "," + Chr(34) + "w" + Chr(34) + ": 200," + Chr(34) + "h" + Chr(34) + ": 200}," + Chr(34) + "thumbnail" + Chr(34) + ":{" + Chr(34) + "fname" + Chr(34) + ": " + Chr(34) + "donutThumb.jpg" + Chr(34) + "," + Chr(34) + "w" + Chr(34) + ": 32," + Chr(34) + "h" + Chr(34) + ": 32}}"

    success = CkJsonObject::ckLoad(json,jsonStr)
    If success <> 1
        Debug CkJsonObject::ckLastErrorText(json)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    ; To pretty-print, set the EmitCompact property equal to 0
    CkJsonObject::setCkEmitCompact(json, 0)

    ; If bare-LF line endings are desired, turn off EmitCrLf
    ; Otherwise CRLF line endings are emitted.
    CkJsonObject::setCkEmitCrLf(json, 0)

    ; Emit the formatted JSON:
    Debug CkJsonObject::ckEmit(json)


    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure