(PureBasic) Extract PDF from JSON
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.
IncludeFile "CkBinData.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Load the JSON.
success.i = 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
|