Download JSON to PDF

71 views
Skip to first unread message

Anderson Cardoso

unread,
Sep 5, 2022, 11:01:32 AMSep 5
to
I am using a webservice that responses with a JSON file with a PDF inside: ["certidaoPDF"].

Response Class (Status 200):
application/json;charset=UTF-8
{
"razaoSocial":"string",
"nomeFantasia":string,
"cnpj":"string",
"uf":string,
"certidoes": [
{
"emissor":"TCU",
"tipo":"string",
"dataHoraEmissao":"13/04/2018 16:38",
"descricao":"string",
"situacao":"string",
"observacao":"string"
}
],
"certidaoPDF":"application/pdf"
}

I am using xHarbour v1.2.1 and there´s no hb_jsonDecode lib so I use was using this function below but it doesn´t work in this case:

Function JSontoHash( cStringJson )
/***
* Convert string Json format to Hash
*/
Local hJson := {=>}
cStringJson := StrTran( cStringJson,':[','=>{')
cStringJson := StrTran( cStringJson,'":"','" => "')
cStringJson := StrTran( cStringJson,'[','{')
cStringJson := StrTran( cStringJson,']','}')
cStringJson := StrTran( cStringJson,'":null','"=>nil')
cStringJson := StrTran( cStringJson,'":true' ,'"=>.t.' )
cStringJson := StrTran( cStringJson,'":false','"=>.f.')
cStringJson := StrTran( cStringJson,'":','"=>')
cStringJson := StrTran( cStringJson,"\/","/" )
hJSon := &( cStringJson )
Return hJson

This is my code:

http := CreateObject("MSXML2.ServerXMLHTTP")
http:Open("GET", "https://certidoes-apf.apps.tcu.gov.br/api/rest/publico/certidoes/"+cCNPJ+"?seEmitirPDF=true", .F.)
http:send()
JSONCNPJ := http:responseText
hCNPJ := JSontoHash(JSONCNPJ) // RETURNS AN ERROR HERE

My objective is to download that PDF.

Anderson Cardoso

unread,
Sep 9, 2022, 11:38:32 PMSep 9
to
Problem solved with hb_jsonDecode() and HB_Base64Decode():

hb_jsonDecode( JSONCNPJ, @hCNPJ )
// SALVA PDF
nFileHandle := FCreate(fCERTIDAO)
FWRITE(nFileHandle, HB_Base64Decode( hCNPJ["certidaoPDF"]) )
FCLOSE(nFileHandle)

Reply all
Reply to author
Forward
0 new messages