Page 1 of 2
JSONtoArray() without indexes?
Posted: Sat Mar 19, 2022 11:18 am
by Zax
Hello,
For this JSON string
Code: Select all
[{"kind": "mp3"},{"version": "2.4"},{"length": "7.266297808012094"},{"bitrate": "153877"},{"hasillustration": "0"},{"unused": "-2"}]
JSONtoArray(jsonString) returns:
Is there some parameters to make JSONtoArray() return an associative array, like it would in Javascript for example and without these annoying indexes?
Thank you.
Re: JSONtoArray() without indexes?
Posted: Sat Mar 19, 2022 6:29 pm
by bobcole
Zax:
I have always had trouble with arrays. I tried working with your sample and found a way to extract the key/value pairs.
In a new stack, I created a field with your JSON string (field "Sample JSON") and a button with the following script.
Code: Select all
on mouseUp
--ws.goulding.script-library.mergjson
--from dictionary: JSONToArray(pJSON)
--JSON arrays are translated to LiveCode numerically indexed arrays with keys 1...N
--and JSON objects are translated to LiveCode array key -> value pairs.
put field "Sample JSON" into tJSON
replace "[" with empty in tJSON
replace "]" with empty in tJSON
replace "{" with empty in tJSON
replace "}" with empty in tJSON
put "{" before tJSON
put "}" after tJSON
put JSONToArray(tJSON) into tArray --the key/value results are not in the original order
put the keys of tArray into tKeys
put the number of lines in the keys of tArray into tKeyCount
put empty into tRow
repeat with j = 1 to tKeyCount
put line j of tKeys into thisKey
put thisKey & ": "& tArray[thisKey] & return after tRow
end repeat
delete the last character of tRow --removes the final return character
put tRow into message box
end mouseUp
As you can see, I found it necessary to remove all the brackets - [ ] { } - and reinsert curly brackets before and after the JSON string. This allowed the JSONtoArray function to work properly.
Hope this helps,
Bob
Re: JSONtoArray() without indexes?
Posted: Sat Mar 19, 2022 8:44 pm
by jacque
I tried this, it seemed to work:
Code: Select all
put jsonToArray(fld 1) into tData
repeat for each key k in tData
combine tData[k] by cr and tab
put tData[k] & cr after tArray
end repeat
split tArray by cr and tab
Re: JSONtoArray() without indexes?
Posted: Sat Mar 19, 2022 9:16 pm
by bobcole
Jacque:
A much more elegant solution!
Thanks,
Bob
Re: JSONtoArray() without indexes?
Posted: Sun Mar 20, 2022 7:54 am
by Zax
Thank you for your answers.
Impressive, Jacqueline!
I love this kind of powerful and minimalist script
EDIT:
I realize I also have deeper JSON strings, like this:
Code: Select all
[{"tags": [{"tracknumber": "11"},{"title": "Shell Blues"},{"artist": "Kurt Vile"},{"album": "Smoke Ring For My Halo"},{"year": "2011"},{"composer": ""},{"genre": "Indie Rock"},{"albumartist": "Kurt Vile"},{"compilation": "1"},{"titlesort": ""},{"albumsort": ""},{"albumartistsort": ""},{"artistsort": ""},{"composersort": ""},{"publisher": "Matador Records"}]},{"kind": "mp3"},{"version": "2.4"},{"length": "01:01"},{"bitrate": "320"},{"hasillustration": "1"},{"unused": "-1"}]
This kind of array requires a recursive version of your script.
Re: JSONtoArray() without indexes?
Posted: Sun Mar 20, 2022 6:16 pm
by FourthWorld
JavaScript objects are not the same as LiveCode's associative arrays. IIRC the indices are added to allow round-tripping.
If the JSON never needs to go back to where it came from the indices can be omitted as requested.
But if the JSON needs to be returned, you'll want to do those mods on a copy for display only, and do any real work with the array as provided.
Re: JSONtoArray() without indexes?
Posted: Mon Mar 21, 2022 8:03 am
by Zax
OK, I now understand why the returned array is indexed.
However, in case of the JSON doesn't need to be returned, the array made by JSONToArray is not convenient at all. I finally wrote this little script to "clean" it (not intensively tested):
Code: Select all
function getJsonToArray_simplify jsonString, arrData
repeat for each element elt in jsonString
repeat for each key tKey in elt
if (elt[tKey] is an array) then
put getJsonToArray_simplify(elt[tKey], arrData) into arrData[tKey]
else put elt[tKey] into arrData[tKey]
end repeat
end repeat
return arrData
end getJsonToArray_simplify
If the JSON string is large, this nested loop can take time to be executed. So maybe the most convenient solution would be to give JSONToArray an optional boolean parameter, something like "returnSimpliedArray"

Re: JSONtoArray() without indexes?
Posted: Mon Mar 21, 2022 8:33 am
by stam
Don’t know if it makes a difference or if that option even exists, but you may want to check out other parsers than the default in case it does.
FerrusLogic have shared their own parser
PhotonJSON as free/opensource.
I haven’t had a need to look at it yet - if the option doesn’t exist you can always add a feature request on their GitHub issues section.
----
EDIT - link above fixed
----
Re: JSONtoArray() without indexes?
Posted: Mon Mar 21, 2022 10:04 am
by Zax
Thanks for the link, stam, I'll have a look at PhotonJSON.
Re: JSONtoArray() without indexes?
Posted: Mon Mar 21, 2022 11:58 am
by stam
Zax wrote: ↑Mon Mar 21, 2022 10:04 am
Thanks for the link,
stam, I'll have a look at PhotonJSON.
just noticed there was something weird with the link i gave it it wouldn't work (now fixed).
The URL is
https://ferruslogic.com/product/PhotonJSON
Github:
https://github.com/Ferruslogic/PhotonJSON
Re: JSONtoArray() without indexes?
Posted: Mon Mar 21, 2022 10:06 pm
by jacque
Zax wrote: ↑Mon Mar 21, 2022 8:03 am
OK, I now understand why the returned array is indexed.
If you have any control over the JSON it would be seamless if it could be changed to return an object instead of an array.
Re: JSONtoArray() without indexes?
Posted: Tue Mar 22, 2022 7:34 am
by Zax
jacque wrote: ↑Mon Mar 21, 2022 10:06 pm
If you have any control over the JSON it would be seamless if it could be changed to return an object instead of an array.
Very interestring, as I have full control on the JSON, generated by one of my Python script. At this time, I use an online JSON parser to verify the returned JSON string and I think that's the source of my problem.
By the way, maybe JSON is not the best way in my case to retreive data.
So, a more general question would be: how could I format a string in order to retrieve data as a LC associative array?
Re: JSONtoArray() without indexes?
Posted: Tue Mar 22, 2022 2:27 pm
by FourthWorld
jacque wrote: ↑Mon Mar 21, 2022 10:06 pm
Zax wrote: ↑Mon Mar 21, 2022 8:03 am
OK, I now understand why the returned array is indexed.
If you have any control over the JSON it would be seamless if it could be changed to return an object instead of an array.
What sort of object do you have in mind?
Re: JSONtoArray() without indexes?
Posted: Tue Mar 22, 2022 2:30 pm
by FourthWorld
Zax wrote: ↑Tue Mar 22, 2022 7:34 am
By the way, maybe JSON is not the best way in my case to retreive data.
So, a more general question would be: how could I format a string in order to retrieve data as a LC associative array?
Where does your Python script obtain the data? In what format does Python receive the data?
Would a simple tab-delimited list suffice?
Re: JSONtoArray() without indexes?
Posted: Tue Mar 22, 2022 3:18 pm
by Zax
In my case, LC send a mp3 file name to a Python script. This script read ID3 tags for the given file, and returns a
string with lots of infos.
I can create this string as I want, in order to be read and parsed by LC.
After parsing the string returned by Python, I would like to have this LC indexed array:
FourthWorld wrote: ↑Tue Mar 22, 2022 2:30 pm
Would a simple tab-delimited list suffice?
I started with that, but it soon appears that it was not a good way.
Once again, JSONToArray works, but returns an
indexed array, that I have to simplify with nested loops.