mergJSONDecode utf-8: where to textDecode

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
trevix
Posts: 425
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

mergJSONDecode utf-8: where to textDecode

Post by trevix » Sat Feb 09, 2019 4:05 pm

Hi all.
Working with the Filemaker 17 Data API, I need to textDecode the content of the returned data, after a call.
This because I am working with accented chars, etc.

I am using the mergJSONDecode to transform the received JSON to an LC array.

Code: Select all

function JSONToArray pJSON
     local tArray,tKeys
     repeat for each line tKey in mergJSONDecode(pJSON,"tArray")
          put JSONToArray(tArray[tKey]) into tArray[tKey]
     end repeat
     return tArray
end JSONToArray
My question is: do I need to textDecode the created array with a new repeat loop OR can I intercept somehow the above routine, without the need to go over each content and each key?
Note that if I "TextDecode" the JSON, the above function won't work.

Thanks
Trevix
Trevix

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4946
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: mergJSONDecode utf-8: where to textDecode

Post by jacque » Sun Feb 10, 2019 6:18 pm

I've never used mergJSONDecode, I usually use jsonImport. I textDecode the whole JSON string first before converting it to a LC array. That seems to work okay. I'd think you could do the same with mergJSON Decode. What doesn't work when you do that?
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

trevix
Posts: 425
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: mergJSONDecode utf-8: where to textDecode

Post by trevix » Mon Feb 11, 2019 1:59 pm

This works alright:
1)

Code: Select all

put "{" & quote & "response" & quote & ":" & quote & "complesso" & quote & "}" into TheJson
     put JSONToArray(TheJson) into tArray
This doesn't:
2)

Code: Select all

  put "{" & quote & "response" & quote & ":" & quote & "più complesso" & quote & "}" into TheJson
     put JSONToArray(TheJson) into tArray
Error on the function: stack "lib_FMS_API": execution error at line n/a (External handler execution error: could not decode JSON: unable to decode byte 0x9d near '"pi') near "could not decode JSON: unable to decode byte 0x9d near '"pi'"

This works, but "response" on the array is: "pi complesso" (missing the accented ù):
3)

Code: Select all

  put "{" & quote & "response" & quote & ":" & quote & "più complesso" & quote & "}" into TheJson
     put textDecode(TheJson,"UTF8") into TheJson
     put JSONToArray(TheJson) into tArray
More: from FileMaker Server API I really receive this Json:
4)

Code: Select all

put "{" & quote & "response" & quote & ":" & quote & "pi√π complesso" & quote & "}" into TheJson
     put textDecode(TheJson,"UTF8") into TheJson
     put JSONToArray(TheJson) into tArray
Erron on the function: stack "lib_FMS_API": execution error at line n/a (External handler execution error: could not decode JSON: unable to decode byte 0x9d near '"pi') near "could not decode JSON: unable to decode byte 0x9d near '"pi'"

BUT if I textDecode the array, instead of the JSON, I get the correct result for the key "response"="più complesso":
5)

Code: Select all

put "{" & quote & "response" & quote & ":" & quote & "pi√π complesso" & quote & "}" into TheJson
     put JSONToArray(TheJson) into tArray
     put textDecode(tArray["response"],"UTF8") into TheResponse
Is there something I have to know if I use jsonimport instead of mergJSONDecode, that clearly has some problem?

Thanks
Trevix

trevix
Posts: 425
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: mergJSONDecode utf-8: where to textDecode

Post by trevix » Mon Feb 11, 2019 6:17 pm

I tried jsonImport and works fine.

BUT jsonExport gives me a problem I don't know how to solve:
Doing a search, FMS requires all search values to be between brackets,even for a single field search:

Code: Select all

{"query":[{"Template_FullName":"Temp _Example 04"}]}
Instead jsonExport return this:

Code: Select all

{"query":{"Template_FullName":"Temp _Example 04"}}
Sure, I could parse it, but for more complex research it would be a nightmare
Shouldn't JSON be a standard?
Trevix

trevix
Posts: 425
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: mergJSONDecode utf-8: where to textDecode

Post by trevix » Mon Feb 11, 2019 6:50 pm

Trevix

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4946
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: mergJSONDecode utf-8: where to textDecode

Post by jacque » Mon Feb 11, 2019 11:02 pm

Interesting bug report you linked to. I've never tried FastJSON but it couldn't hurt for you to give it a whirl. if you do decide to use it, let us know, I may change my technique in the future. ;)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

trevix
Posts: 425
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: mergJSONDecode utf-8: where to textDecode

Post by trevix » Tue Feb 12, 2019 1:23 am

It seems to work fine for ArrayToJson, solving my problem.
For JasonToArray it doesn't fit (error with accented chars) and I have to use JsonImport(tesxtDecode(tJson,"uff-8")).

The only problem with FastJson is that has a lot of unidecode and unidecode (both deprecated): I don't understand how they work and how to fix them.
Trevix

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4946
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: mergJSONDecode utf-8: where to textDecode

Post by jacque » Tue Feb 12, 2019 6:20 pm

Well, "deprecated" doesn't mean "gone". You can still safely use it. We're encouraged not to, but those functions will be around for a while.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

trevix
Posts: 425
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: mergJSONDecode utf-8: where to textDecode

Post by trevix » Sat Feb 16, 2019 3:03 pm

Just a note on Json and data Api:

While trying to create a new record on filemaker, to holds a file (in a container) and some info of it, I couldn't resolve it until I found procedure:
- create an array with the info
- load the binary of the file into a var
- encrypt it (because I need)
- base64Encode it and put it in the array (otherwise the "arrayToJson" trows an error)
- arrayToJson(the array) using fastJson
- textEncode(the JSon, "uff-8") because so required by FM
- post it

The same in reverse must be done to download the array and save the file.

Geee...I will never understand all this unicode, utf-8, utf-16,base64, etc. stuff.
And, anyway, JSON sucks.
Trevix

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”