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"}
/*
* 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
/*
* 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