JSONToArray Bug

If you find an issue in LiveCode but are having difficulty pinning down a reliable recipe or want to sanity-check your findings with others, this is the place.

Please have one thread per issue, and try to summarize the issue concisely in the thread title so others can find related issues here.

Moderator: Klaus

Post Reply
TR-i
Posts: 18
Joined: Fri Jul 21, 2006 4:06 am

JSONToArray Bug

Post by TR-i » Thu Jan 20, 2022 3:26 am

When converting a JSON string to a Livecode array, empty Javascript arrays are converted to plain empty variables. This causes errors when converting back to JSON (then parsing in Javascript). Javascript expects an empty array to be 2 brackets... []. Executing code will no longer see the variable as an array.

andresdt
Posts: 156
Joined: Fri Aug 16, 2019 7:51 pm
Contact:

Re: JSONToArray Bug

Post by andresdt » Thu Jan 20, 2022 5:34 pm

Hi @TR-i

Yesterday when we read your post, at first it seemed to us that it was not a mistake :oops:
Rather, it was about how data is represented in LiveCode vs JavaScript. But then we wonder if it would be possible to represent an empty array in LiveCode.

Well, if you can:

Code: Select all

put empty in tArray[1]
This would fix the problem with [] representing arrays in JS. But we were missing the empty JS objects {}.

Code: Select all

put empty in tArray[empty]
And that's it, this way we can prevent JSON arrays and objects from being converted to plain variables.
So we have updated our JSON handling library with this feature.

You can find it here: https://ferruslogic.com/product/PhotonJSON

If PhotonJSON helps you solve your problem and if you think the project deserves it. Leave us a star on github, to continue developing this library.
https://github.com/Ferruslogic/PhotonJSON
Be kind, we all have our own wars.
https://torocruzand.com/

stam
Posts: 3061
Joined: Sun Jun 04, 2006 9:39 pm

Re: JSONToArray Bug

Post by stam » Thu Jan 20, 2022 7:04 pm

very nice @andresdt!

andresdt
Posts: 156
Joined: Fri Aug 16, 2019 7:51 pm
Contact:

Re: JSONToArray Bug

Post by andresdt » Thu Jan 20, 2022 8:30 pm

Thank you @Stam on behalf of myself and my partner @prometheus. For your words and for your star on Github :D
Be kind, we all have our own wars.
https://torocruzand.com/

TR-i
Posts: 18
Joined: Fri Jul 21, 2006 4:06 am

Re: JSONToArray Bug

Post by TR-i » Fri Jan 21, 2022 2:26 am

Converting JSON to array seems to be working correctly, but not back to JSON.

This going in: "list":{}

Becomes this when converted back to JSON: "list":{"1":"","":""}

andresdt
Posts: 156
Joined: Fri Aug 16, 2019 7:51 pm
Contact:

Re: JSONToArray Bug

Post by andresdt » Fri Jan 21, 2022 2:52 am

Was that with the library? :?: :?: :?:
Be kind, we all have our own wars.
https://torocruzand.com/

andresdt
Posts: 156
Joined: Fri Aug 16, 2019 7:51 pm
Contact:

Re: JSONToArray Bug

Post by andresdt » Fri Jan 21, 2022 4:02 am

I have tried the example you provide and here it works OK

Code: Select all

put JSONStringify(JSONParser( "{"& quote &"list"& quote &":{}}" ))  --> Returns {"list": {}} 
Be kind, we all have our own wars.
https://torocruzand.com/

TR-i
Posts: 18
Joined: Fri Jul 21, 2006 4:06 am

Re: JSONToArray Bug

Post by TR-i » Fri Jan 21, 2022 6:42 am

Example should have been “list”:[] (brackets, not curly braces).

Looks like arrays (brackets) are being converted to objects (curly braces).

andresdt
Posts: 156
Joined: Fri Aug 16, 2019 7:51 pm
Contact:

Re: JSONToArray Bug

Post by andresdt » Fri Jan 21, 2022 2:02 pm

TR-i wrote:
Fri Jan 21, 2022 6:42 am
Example should have been “list”:[] (brackets, not curly braces).

Looks like arrays (brackets) are being converted to objects (curly braces).
Hi @TR-i

You are using the latest version of the PhotonJSON library?

https://github.com/Ferruslogic/PhotonJS ... codescript

try with

Code: Select all

put JSONStringify(JSONParser( "{"& quote &"list"& quote &":[]}" )) --> {"list": []}
It worked well for me.
Please confirm whether or not you are using the PhotonJSON library. Since this was where this was fixed.
Be kind, we all have our own wars.
https://torocruzand.com/

TR-i
Posts: 18
Joined: Fri Jul 21, 2006 4:06 am

Re: JSONToArray Bug

Post by TR-i » Fri Feb 04, 2022 11:29 pm

Yes, using the latest.

Translating back and forth inside Livecode would obviously work. However, results are inconsistent when you actually hand the JSON to Javascript to translate back into objects.

Also, while I installed the library using the installer stack, standalone apps failed to find it.

Post Reply