A snippet floating around the net uses the Foundation framework and NSJSONSerialization to serialize AppleScript records. However it chokes when the record was generated by Music.app.
Any idea of how to fix this? I have some experience with AS, but nothing with Cocoa.
I have also tried using “JSON Helper” from the App store, but it just returns an empty string.
use framework "Foundation"
-- pass a string, list, record or number, and either a path to save the result to, or missing value to have it returned as text
on convertASToJSON(someASThing)
--convert to JSON data
set {theData, theError} to current application's NSJSONSerialization's dataWithJSONObject:someASThing options:0 |error|:(reference)
if theData is missing value then error (theError's localizedDescription() as text) number -10000
-- convert data to a UTF8 string
set someString to current application's NSString's alloc()'s initWithData:theData encoding:(current application's NSUTF8StringEncoding)
return someString as text
end convertASToJSON
set j to convertASToJSON({a:1, b:2})
log j -- works!
tell application "Music"
set p to properties of track 1
log my convertASToJSON(p) -- *** +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write
end tell