• Download JSON to PDF

    From Anderson Cardoso@21:1/5 to All on Mon Sep 5 08:01:31 2022
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Anderson Cardoso@21:1/5 to All on Fri Sep 9 20:38:31 2022
    Em segunda-feira, 5 de setembro de 2022 às 12:01:32 UTC-3, Anderson Cardoso escreveu:
    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.

    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)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)