Sample code for 30+ languages & platforms
PureBasic

Extract PDF from JSON

See more JSON Examples

Demonstrates how to extract a PDF file contained within JSON. The file is represented as a base64 string within the JSON. Note: This example can extract any type of file, not just a PDF file.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

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

    ; Load the JSON.
    success = CkJsonObject::ckLoadFile(json,"qa_data/json/JSR5U.json")
    If success <> 1
        Debug CkJsonObject::ckLastErrorText(json)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    ; The JSON we loaded contains this:

    ; 	{
    ; 	...
    ; 	...
    ; 	  "data": {
    ; 	    "content": "JVBERi0xLjQ..."
    ; 	  }
    ; 	...
    ; 	...
    ; 	}

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

    CkJsonObject::ckStringOfSb(json,"data.content",sb)

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

    CkBinData::ckAppendEncodedSb(bd,sb,"base64")

    success = CkBinData::ckWriteFile(bd,"qa_output/a0015.pdf")


    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sb)
    CkBinData::ckDispose(bd)


    ProcedureReturn
EndProcedure