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: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
Zax
- Posts: 519
- Joined: Mon May 28, 2007 10:12 am
-
Contact:
Post
by Zax » Sat Mar 19, 2022 11:18 am
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.
-
bobcole
- VIP Livecode Opensource Backer

- Posts: 161
- Joined: Tue Feb 23, 2010 10:53 pm
Post
by bobcole » Sat Mar 19, 2022 6:29 pm
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
-
jacque
- VIP Livecode Opensource Backer

- Posts: 7390
- Joined: Sat Apr 08, 2006 8:31 pm
-
Contact:
Post
by jacque » Sat Mar 19, 2022 8:44 pm
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
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
-
bobcole
- VIP Livecode Opensource Backer

- Posts: 161
- Joined: Tue Feb 23, 2010 10:53 pm
Post
by bobcole » Sat Mar 19, 2022 9:16 pm
Jacque:
A much more elegant solution!
Thanks,
Bob
-
Zax
- Posts: 519
- Joined: Mon May 28, 2007 10:12 am
-
Contact:
Post
by Zax » Sun Mar 20, 2022 7:54 am
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.
-
FourthWorld
- VIP Livecode Opensource Backer

- Posts: 10045
- Joined: Sat Apr 08, 2006 7:05 am
-
Contact:
Post
by FourthWorld » Sun Mar 20, 2022 6:16 pm
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.
-
Zax
- Posts: 519
- Joined: Mon May 28, 2007 10:12 am
-
Contact:
Post
by Zax » Mon Mar 21, 2022 8:03 am
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"

-
stam
- Posts: 3069
- Joined: Sun Jun 04, 2006 9:39 pm
Post
by stam » Mon Mar 21, 2022 8:33 am
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
----
Last edited by
stam on Mon Mar 21, 2022 11:58 am, edited 4 times in total.
-
Zax
- Posts: 519
- Joined: Mon May 28, 2007 10:12 am
-
Contact:
Post
by Zax » Mon Mar 21, 2022 10:04 am
Thanks for the link, stam, I'll have a look at PhotonJSON.
-
jacque
- VIP Livecode Opensource Backer

- Posts: 7390
- Joined: Sat Apr 08, 2006 8:31 pm
-
Contact:
Post
by jacque » 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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
-
Zax
- Posts: 519
- Joined: Mon May 28, 2007 10:12 am
-
Contact:
Post
by Zax » Tue Mar 22, 2022 7:34 am
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?
-
FourthWorld
- VIP Livecode Opensource Backer

- Posts: 10045
- Joined: Sat Apr 08, 2006 7:05 am
-
Contact:
Post
by FourthWorld » Tue Mar 22, 2022 2:27 pm
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?
-
FourthWorld
- VIP Livecode Opensource Backer

- Posts: 10045
- Joined: Sat Apr 08, 2006 7:05 am
-
Contact:
Post
by FourthWorld » Tue Mar 22, 2022 2:30 pm
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?
-
Zax
- Posts: 519
- Joined: Mon May 28, 2007 10:12 am
-
Contact:
Post
by Zax » Tue Mar 22, 2022 3:18 pm
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.