Confusing NSJSONSerializationData error

So I’m testing out pulling down JSON data from a site. That works well, data comes in, and if do a lot of grep tests, I can get what I need somewhat easily.

But, if I try to use it with NSJSONSerializationData, I get errors that I’m not sure about what they mean.

code example:

set theReturn to do shell script "/usr/bin/curl -XGET \"" & theServerURL & theServerAPIKey & "&pretty=1\""

(this works, brings down a gob of JSON data as text)

set theJSONDict to current application's NSJSONSerialization's JSONObjectWithData:theReturn options:0 |error|:(missing value)

This is where things go screwy. When I test this in ScriptDebugger, I get: -[__NSCFString bytes]: unrecognized selector sent to instance 0x60000023b500

I’ve tried a few things with theReturn, forcing it to be different variable types, no love. I’ve set options to 0, 1, 2, (missing value), no change. Well, one change, if I set it to missing value, I get a different error message: unable to set argument 3 - the AppleScript value <NSAppleEventDescriptor: ‘msng’> could not be coerced to type Q.

so I’m pretty sure missing value is bad there.

The JSON data that comes down in the curl step seems to be okay, i can post that if it helps.

You’re passing a string where data is required. Try something like this:

set theReturn to do shell script "/usr/bin/curl -XGET \"" & theServerURL & theServerAPIKey & "&pretty=1\""
set theReturn to current application's NSString's stringWithString:theReturn
set theData to theReturn's dataUsingEncoding:(current application's NSUTF8StringEncoding)
set {theJSONDict, theError} to current application's NSJSONSerialization's JSONObjectWithData:theData options:0 |error|:(reference)
if theJSONDict = missing value then 
current application's NSLog("Error: %@", theError's localizedDescription())
-- handle error

DOH!

Says right there in the friggin’ docs I need to pass the JSON data as NSData, totally muffed my reading comprehension roll on that one.

thanks man.