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!
Is there some parameters to make JSONtoArray() return an associative array, like it would in Javascript for example and without these annoying indexes?
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.
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
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
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):
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"
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?
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?
Richard Gaskin LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
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.