Json result

Post Reply
wartiaga
Posts: 254
Joined: Wed May 25, 2016 1:04 am

Json result

Post by wartiaga »

Hi everyone,

I'm integrating with a bank in Brazil to register payment slips, and it's returning a JSON with the following information. How can I change the text to correct accents and Portuguese (Brazil) characters? Thank you.

{"type":"https://developers.c6bank.com.br/v1/err ... quisição inválida.","status":400,"detail":"[BoletoClient]: Erro ao inserir registro: Já existe o título 10201374 com o seu número TESTE002 cadastrado . Bad Request","correlation_id":"9f25b39d97dc1b26-GRU","timestamp":"2026-04-26T12:48:27.169Z"}

User avatar
Antonio Linares
Site Admin
Posts: 44515
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Json result

Post by Antonio Linares »

Code: Select all | Expand

/*  
 * Program to fix Portuguese character encoding in JSON responses  
 * from Brazilian bank API integration  
*/
#include "hbjson.ch"
PROCEDURE Main()
// Set language to Portuguese Brazil for proper character support
hb_langSelect("PT_BR")
// Sample garbled JSON response from the bank
cRawJson := '{"type":"https://developers.c6bank.com.br/v1/err ... quisição inválida.",' + ;
'"status":400,' + ;
'"detail":"[BoletoClient]: Erro ao inserir registro: Já existe o título 10201374 com o seu número TESTE002 cadastrado . Bad Request",' + ;
'"correlation_id":"9f25b39d97dc1b26-GRU",' + ;
'"timestamp":"2026-04-26T12:48:27.169Z"}'
? "Original garbled JSON:"
? cRawJson
? ""
// Method 1: Automatic OS encoding conversion
? "Method 1: Using hb_osStrDecode() for automatic conversion:"
cFixedJson1 := FixJsonEncoding(cRawJson)
? cFixedJson1
? ""
// Method 2: Specific codepage conversion (ISO-8859-1)
? "Method 2: Using PTISO codepage conversion:"
cFixedJson2 := FixJsonEncodingWithCodepage(cRawJson, "PTISO")
? cFixedJson2
? ""
// Parse and display the corrected JSON
? "Parsed JSON content:"
ParseAndDisplayJson(cFixedJson1)
RETURN
STATIC FUNCTION FixJsonEncoding(cJsonString)
// Convert from system encoding to Harbour internal encoding
LOCAL cFixed := hb_osStrDecode(cJsonString)
RETURN cFixed
STATIC FUNCTION FixJsonEncodingWithCodepage(cJsonString, cCodepage)
// Convert using specific codepage
LOCAL cFixed := hb_cdpStrToUTF8(cJsonString, cCodepage)
RETURN cFixed
STATIC PROCEDURE ParseAndDisplayJson(cJsonString)
LOCAL hJson, nDecoded
// Decode JSON to Harbour hash
nDecoded := hb_jsonDecode(cJsonString, @hJson)
IF nDecoded > 0 .AND. HB_ISHASH(hJson)
? "Type:", hJson["type"]
? "Status:", hJson["status"]
? "Detail:", hJson["detail"]
? "Correlation ID:", hJson["correlation_id"]
? "Timestamp:", hJson["timestamp"]
ELSE
? "Failed to parse JSON"
ENDIF
RETURN
regards, saludos

Antonio Linares
www.fivetechsoft.com
wartiaga
Posts: 254
Joined: Wed May 25, 2016 1:04 am

Re: Json result

Post by wartiaga »

Antonio Linares wrote: Sun Apr 26, 2026 2:10 pm

Code: Select all | Expand

/*  
 * Program to fix Portuguese character encoding in JSON responses  
 * from Brazilian bank API integration  
*/
#include "hbjson.ch"
PROCEDURE Main()
// Set language to Portuguese Brazil for proper character support
hb_langSelect("PT_BR")
// Sample garbled JSON response from the bank
cRawJson := '{"type":"https://developers.c6bank.com.br/v1/err ... quisição inválida.",' + ;
'"status":400,' + ;
'"detail":"[BoletoClient]: Erro ao inserir registro: Já existe o título 10201374 com o seu número TESTE002 cadastrado . Bad Request",' + ;
'"correlation_id":"9f25b39d97dc1b26-GRU",' + ;
'"timestamp":"2026-04-26T12:48:27.169Z"}'
? "Original garbled JSON:"
? cRawJson
? ""
// Method 1: Automatic OS encoding conversion
? "Method 1: Using hb_osStrDecode() for automatic conversion:"
cFixedJson1 := FixJsonEncoding(cRawJson)
? cFixedJson1
? ""
// Method 2: Specific codepage conversion (ISO-8859-1)
? "Method 2: Using PTISO codepage conversion:"
cFixedJson2 := FixJsonEncodingWithCodepage(cRawJson, "PTISO")
? cFixedJson2
? ""
// Parse and display the corrected JSON
? "Parsed JSON content:"
ParseAndDisplayJson(cFixedJson1)
RETURN
STATIC FUNCTION FixJsonEncoding(cJsonString)
// Convert from system encoding to Harbour internal encoding
LOCAL cFixed := hb_osStrDecode(cJsonString)
RETURN cFixed
STATIC FUNCTION FixJsonEncodingWithCodepage(cJsonString, cCodepage)
// Convert using specific codepage
LOCAL cFixed := hb_cdpStrToUTF8(cJsonString, cCodepage)
RETURN cFixed
STATIC PROCEDURE ParseAndDisplayJson(cJsonString)
LOCAL hJson, nDecoded
// Decode JSON to Harbour hash
nDecoded := hb_jsonDecode(cJsonString, @hJson)
IF nDecoded > 0 .AND. HB_ISHASH(hJson)
? "Type:", hJson["type"]
? "Status:", hJson["status"]
? "Detail:", hJson["detail"]
? "Correlation ID:", hJson["correlation_id"]
? "Timestamp:", hJson["timestamp"]
ELSE
? "Failed to parse JSON"
ENDIF
RETURN

Thank you Antonio but I use xharbour, any similar function?
Error: Unresolved external 'HB_FUN_HB_OSSTRDECODE' referenced from D:\LANG\FWH1811\FWH1811\SAMPLES\C6\C6.OBJ
Error: Unresolved external 'HB_FUN_HB_CDPSTRTOUTF8' referenced from D:\LANG\FWH1811\FWH1811\SAMPLES\C6\C6.OBJ

User avatar
Antonio Linares
Site Admin
Posts: 44515
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Json result

Post by Antonio Linares »

Code: Select all | Expand

/*    
 * Program to fix Portuguese character encoding in JSON responses    
 * from Brazilian bank API integration - xHarbour version  
*/
PROCEDURE Main()
// Set language to Portuguese Brazil for proper character support
hb_langSelect("PT_BR")
// Sample garbled JSON response from the bank
cRawJson := '{"type":"https://developers.c6bank.com.br/v1/err ... quisição inválida.",' + ;
'"status":400,' + ;
'"detail":"[BoletoClient]: Erro ao inserir registro: Já existe o título 10201374 com o seu número TESTE002 cadastrado . Bad Request",' + ;
'"correlation_id":"9f25b39d97dc1b26-GRU",' + ;
'"timestamp":"2026-04-26T12:48:27.169Z"}'
? "Original garbled JSON:"
? cRawJson
? ""
// Method 1: Automatic OS encoding conversion
? "Method 1: Using hb_osStrDecode() for automatic conversion:"
cFixedJson1 := FixJsonEncoding(cRawJson)
? cFixedJson1
? ""
// Method 2: Specific codepage conversion (ISO-8859-1)
? "Method 2: Using PTISO codepage conversion:"
cFixedJson2 := FixJsonEncodingWithCodepage(cRawJson, "PTISO")
? cFixedJson2
? ""
// Parse and display the corrected JSON
? "Parsed JSON content:"
ParseAndDisplayJson(cFixedJson1)
RETURN
STATIC FUNCTION FixJsonEncoding(cJsonString)
// Convert from system encoding to Harbour internal encoding
LOCAL cFixed := hb_osStrDecode(cJsonString)
RETURN cFixed
STATIC FUNCTION FixJsonEncodingWithCodepage(cJsonString, cCodepage)
// Convert using specific codepage
LOCAL cFixed := hb_cdpStrToUTF8(cJsonString, cCodepage)
RETURN cFixed
STATIC PROCEDURE ParseAndDisplayJson(cJsonString)
LOCAL hJson, nDecoded
// Decode JSON to Harbour hash
nDecoded := hb_jsonDecode(cJsonString, @hJson)
IF nDecoded > 0 .AND. HB_ISHASH(hJson)
? "Type:", hJson["type"]
? "Status:", hJson["status"]
? "Detail:", hJson["detail"]
? "Correlation ID:", hJson["correlation_id"]
? "Timestamp:", hJson["timestamp"]
ELSE
? "Failed to parse JSON"
ENDIF
RETURN
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
karinha
Posts: 8720
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil
Contact:

Re: Json result

Post by karinha »

Master, Build 12 del FWH26.03

Code: Select all | Expand

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ FiveWin for xHarbour 26.03 - Mar. 2026          Harbour development power  ³Ü
³ (c) FiveTech 1993-2026 for Microsoft Windows 9X/NT/200X/ME/XP/Vista/7/8/10 ³Û
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙÛ
ÿ ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß
Compiling...
xHarbour 1.3.2 Intl. (SimpLex) (Build 20260103)
Copyright 1999-2026, http://www.xharbour.org http://www.harbour-project.org/
Compiling 'jsonbr.prg' and generating preprocessed output to 'jsonbr.ppo'...

Generating C source output to 'jsonbr.c'...
Done.

Lines 71, Functions/Procedures 4, pCodes 192

jsonbr.prg(19) Warning W0001  Ambiguous reference: 'CRAWJSON'

jsonbr.prg(22) Warning W0001  Ambiguous reference: 'CRAWJSON'

jsonbr.prg(27) Warning W0001  Ambiguous reference: 'CRAWJSON'

jsonbr.prg(27) Warning W0001  Ambiguous reference: 'CFIXEDJSON1'

jsonbr.prg(28) Warning W0001  Ambiguous reference: 'CFIXEDJSON1'

jsonbr.prg(33) Warning W0001  Ambiguous reference: 'CRAWJSON'

jsonbr.prg(33) Warning W0001  Ambiguous reference: 'CFIXEDJSON2'

jsonbr.prg(34) Warning W0001  Ambiguous reference: 'CFIXEDJSON2'

jsonbr.prg(39) Warning W0001  Ambiguous reference: 'CFIXEDJSON1'
Embarcadero C++ 7.70 for Win32 Copyright (c) 1993-2023 Embarcadero Technologies, Inc.
jsonbr.c:
Turbo Incremental Link 6.97 Copyright (c) 1997-2022 Embarcadero Technologies, Inc.
Error: Unresolved external '_HB_FUN_HB_OSSTRDECODE' referenced from C:\FWH2603\SAMPLES\JSONBR.OBJ
Error: Unresolved external '_HB_FUN_HB_CDPSTRTOUTF8' referenced from C:\FWH2603\SAMPLES\JSONBR.OBJ
Error: Unable to perform link
* Linking errors *

Regards, saludos.

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
karinha
Posts: 8720
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil
Contact:

Re: Json result

Post by karinha »

Maestro, mira que excelente ejemplo.

Code: Select all | Expand

// C:\FWH2603\SAMPLES\JSONBR2.PRG -> FWH26.03 BUILD 12.

#include "fivewin.ch"

// REQUEST HB_CODEPAGE_PTISO // Load necessary codepage
EXTERNAL HB_LANG_PT, HB_CODEPAGE_PTISO, HB_CODEPAGE_PT850

FUNCTION Main()

   LOCAL cJson, cUtf8Json

   HB_LANGSELECT( 'PT' )
   HB_SETCODEPAGE( "UTF8" )
   HB_CDPSELECT( "UTF8EX" )
   HB_SETCODEPAGE( "PT850" )
   HB_CDPSELECT( "PTISO" )

   // hb_langSelect("PT_BR") // INVALID LANGUAGE // ??? NO EJISTE. jajaja.

   FW_SETUNICODE( .T. )

   // String in local encoding (e.g., ISO-8859-1/PT850)
   cJson := '{ "Nome": "João ", "Cidade ": "São Paulo." }'
    
// Convert to UTF-8 for Web API // cUtf8Json := HB_StrToUTF8( cJson, "PT850" ) // ASI, NO FUNCIONA. // VERY GOOD. cUtf8Json := HB_StrToUTF8( cJson, "PTISO" ) // ASI, FUNCIONA BIEN.
? "Original:", cJson // funciona acentuacion ? "UTF-8:", cUtf8Json // funciona acentuacion con "PTISO", "PT850" -> NO. RETURN NIL // FIN / END

Regards, saludos.

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
Antonio Linares
Site Admin
Posts: 44515
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Json result

Post by Antonio Linares »

karinha wrote: Mon Apr 27, 2026 4:16 pm

Master, Build 12 del FWH26.03

Code: Select all | Expand

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ FiveWin for xHarbour 26.03 - Mar. 2026          Harbour development power  ³Ü
³ (c) FiveTech 1993-2026 for Microsoft Windows 9X/NT/200X/ME/XP/Vista/7/8/10 ³Û
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙÛ
ÿ ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß
Compiling...
xHarbour 1.3.2 Intl. (SimpLex) (Build 20260103)
Copyright 1999-2026, http://www.xharbour.org http://www.harbour-project.org/
Compiling 'jsonbr.prg' and generating preprocessed output to 'jsonbr.ppo'...

Generating C source output to 'jsonbr.c'...
Done.

Lines 71, Functions/Procedures 4, pCodes 192

jsonbr.prg(19) Warning W0001  Ambiguous reference: 'CRAWJSON'

jsonbr.prg(22) Warning W0001  Ambiguous reference: 'CRAWJSON'

jsonbr.prg(27) Warning W0001  Ambiguous reference: 'CRAWJSON'

jsonbr.prg(27) Warning W0001  Ambiguous reference: 'CFIXEDJSON1'

jsonbr.prg(28) Warning W0001  Ambiguous reference: 'CFIXEDJSON1'

jsonbr.prg(33) Warning W0001  Ambiguous reference: 'CRAWJSON'

jsonbr.prg(33) Warning W0001  Ambiguous reference: 'CFIXEDJSON2'

jsonbr.prg(34) Warning W0001  Ambiguous reference: 'CFIXEDJSON2'

jsonbr.prg(39) Warning W0001  Ambiguous reference: 'CFIXEDJSON1'
Embarcadero C++ 7.70 for Win32 Copyright (c) 1993-2023 Embarcadero Technologies, Inc.
jsonbr.c:
Turbo Incremental Link 6.97 Copyright (c) 1997-2022 Embarcadero Technologies, Inc.
Error: Unresolved external '_HB_FUN_HB_OSSTRDECODE' referenced from C:\FWH2603\SAMPLES\JSONBR.OBJ
Error: Unresolved external '_HB_FUN_HB_CDPSTRTOUTF8' referenced from C:\FWH2603\SAMPLES\JSONBR.OBJ
Error: Unable to perform link
* Linking errors *

Regards, saludos.

Estimado Joao,

Este ejemplo es para Harbour

regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply