Page 1 of 1
change special chars in memo for use with Json
Posted: Thu Nov 28, 2024 8:24 am
by Marc Venken
I need to convert data inside memo fields to upload for a webshop. Therefore I put the data inside a Json string. This works but more than once it seems that there are
special chars inside the memo field that will confuse the Json string so that the upload is not possible.
I try to convert with StrTran, but is this the correct way ?
Mayby there are
functions to do it for all special chars ?
Code: Select all | Expand
cData = STRTRAN(artikel->foldertxt, chr(13)+chr(10), "<BR>") // memofield
cData = STRTRAN(cData, 'Â', '')
cData = STRTRAN(cData, 'ú', '')
cData = STRTRAN(cData, 'ë', 'ë')
cData = STRTRAN(cData, ';', ' ')
cData = STRTRAN(cData, '"', '')
cData = STRTRAN(cData, 'é', 'é')
cData = STRTRAN(cData, 'è', 'è')
cData = STRTRAN(cData, '®', '')
// process cData inside Json = ok
Re: change special chars in memo for use with Json
Posted: Thu Nov 28, 2024 9:36 am
by Otto
Hello Marc,
my daughter sent me these lines of code a few days ago, saying that they would solve the code page problem.
Unfortunately, I haven’t tried it yet. Could you please test it?
Best regards,
Otto
Code: Select all | Expand
#include "hbextcdp.ch"
cVData := IIF( ! isUtf8( offert1->bezeichnun ), hb_StrToUtf8( ALLTRIM( offert1->bezeichnun ) ), ALLTRIM( offert1->bezeichnun ) )
Re: change special chars in memo for use with Json
Posted: Thu Nov 28, 2024 11:10 am
by Marc Venken
Otto,
This code will change data to UTF8-coded data, but will not change a chr(10) to something else as far as I read.
Re: change special chars in memo for use with Json
Posted: Fri Nov 29, 2024 7:31 am
by Ruth
Dear Marc,
I am very interested in this topic, because those things happen to me a lot
how do you build the json string?
I use hb_JsonEncode() and usually special characters like " and \ are treated very well inside of it.
kind regards,
Ruth
Re: change special chars in memo for use with Json
Posted: Fri Nov 29, 2024 8:16 am
by Marc Venken
Ruth,
I build my Json the old way... There will be
functions I suppose but this works for me.
aVelden = array with selected fields to update the online shop.
Code: Select all | Expand
cBuffer := '{'
cBuffer = cBuffer + '"model":"'+cCode+'",' // Geen ""
IF AScan( aVelden, "NAAM") > 0 // Ben ik actief gezet als status
cData = change_memotxt(alltrim(webshop->naamshop))
cBuffer = cBuffer + '"name":"'+cData+'",' // Geen ""
endif
IF AScan( aVelden, "FABRIKANT") > 0
cBuffer = cBuffer + '"manufacturer":{"id":"'+alltrim(webshop->fabrikan)+'"},'
endif
IF AScan( aVelden, "MEMOTXT") > 0
cLayover = alltrim(webshop->memotxt) // At this point I try to remove chars that are not OK
cLayover = STRTRAN(cLayover, chr(13)+chr(10) , "<br>") // \n
cLayover = STRTRAN(cLayover, chr(10) , "<br>") // \n
cLayover = STRTRAN(cLayover, chr(13) , "<br>") // \r
cLayover = STRTRAN(cLayover, chr(12) , "\f") // Formfeed
if empty(cLayover)
cLayover = ""
endif
if !IsJson(cLayover)
aadd(aFouten,alltrim(webshop->id)+" : Jason Code 'MEMO' is niet conform")
else
cBuffer = cBuffer + '"description":"'+cLayover+'",' // Geen ""
endif
endif
// ..... For every needed field a extra IF
cBuffer = substr(cBuffer,1,len(cBuffer)-1)
cBuffer = cBuffer + '}'
//JsonView( cBuffer )
cUrl="https://YourShop/api/v2/products/"+alltrim(cOnlineCode)+"?token=...."
oHttp := FWGetOleObject( "WINHTTP.WinHttpRequest.5.1" )
WITH OBJECT oHttp
:SetTimeouts(0, 60000, 30000, 120000)
:Open( "PATCH", cUrl, .f. )
:SetRequestHeader( "Accept", "application/json" )
:SetRequestHeader( "Content-Type", "application/json" )
:Send( cBuffer )
:WaitForResponse()
if :status <> 200
aadd(aFouten,"Fout !! : "+webshop->ID+" "+:Statustext)
else
aadd(aFouten,"Artikel : "+webshop->ID+" Updated : "+uResponse)
endif
END
xbrowser(aFouten) title "Fouten"
Re: change special chars in memo for use with Json
Posted: Fri Nov 29, 2024 8:22 am
by Marc Venken
I see the function.... Maybe I should better look at it and start using it.
hb_jsonEncode( { "key1" => "value1", "key2" => "value2", "key3" => "value3" } )
In my memofield there are chr(10) and sometimes other. Will that function change these automatic ?
The way I do it is also handy in my case because I change the char(10) to <br> in order to have a html document.
I also have to look at this sample.
cJson:=HB_JsonEncode(hHash)
Making a Hash from the data will be a easier way that my way .... Learning allready ))))
Re: change special chars in memo for use with Json
Posted: Fri Nov 29, 2024 5:26 pm
by nageswaragunupudi
Code: Select all | Expand
function jsonTest()
local c, h, j
HB_CDPSELECT( "DEWIN" ) // or any
c := "abc" + CRLF + "d ÄÖÜ" // ANSI
h := { "key1"=> If( IsUtf8( c ), c, hb_strtoutf8( c ) ) }
XBROWSER h SETUP oBrw:nDataLines := 3
j := hb_jsonEncode( h )
msginfo( j )
return nil
Re: change special chars in memo for use with Json
Posted: Sat Nov 30, 2024 7:56 am
by Ruth
Dear Mr. Rao,
thank you so much for providing the example. Seeing it in this compact and clear way is really beautiful.
Dear Marc, I admit that for me as a newbie pre-made
functions are simply great and convenient.
But your approach, building from scratch is very educational for me and I understand much more through it now .... thank you.
I would have a follow-up question please:
is there a slimmer way of handling the different elements in cVData? Or is it correct to go through it element by element sanitazing with hb_strtoutf8()
kind regards to you all and have a nice weekend
Ruth
Code: Select all | Expand
cVData := STRTRAN(ALLTRIM(oKunden:anrede), ";","," ) +";"+; // 1 ANREDE
STRTRAN(ALLTRIM(oKunden:name), ";","," ) +";"+; // 2 NAME
STRTRAN(ALLTRIM(oKunden:vorname), ";","," ) +";"+; // 3 VORNAME
STRTRAN(ALLTRIM(oKunden:strasse), ";","," ) +";"+; // 4 STRASSE
STRTRAN(ALLTRIM(oKunden:lkz), ";","," ) +";"+; // 5 LKZ
STRTRAN(ALLTRIM(oKunden:plz), ";","," ) +";"+; // 6 PLZ
STRTRAN(ALLTRIM(oKunden:ort), ";","," ) // 7 ORT