For this JSON string
Code: Select all
[{"kind": "mp3"},{"version": "2.4"},{"length": "7.266297808012094"},{"bitrate": "153877"},{"hasillustration": "0"},{"unused": "-2"}]
Thank you.
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Code: Select all
[{"kind": "mp3"},{"version": "2.4"},{"length": "7.266297808012094"},{"bitrate": "153877"},{"hasillustration": "0"},{"unused": "-2"}]
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
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
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"}]
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
just noticed there was something weird with the link i gave it it wouldn't work (now fixed).
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.
What sort of object do you have in mind?
Where does your Python script obtain the data? In what format does Python receive the data?
I started with that, but it soon appears that it was not a good way.