Re: Change a aJSON list to an Applescript list?
Re: Change a aJSON list to an Applescript list?
- Subject: Re: Change a aJSON list to an Applescript list?
- From: "Nigel Garvey" <email@hidden>
- Date: Sat, 9 Sep 2017 09:53:35 +0100
Brian Christmas wrote on Sat, 9 Sep 2017 02:41:51 +1000:
>… need to check monthly if the serial number for each App is still in
>FastSprings valid serials active list.
>
>This list is downloadable as a JSON file, and I’m trying to work out
how to
>interpret that information into an Applescript list.
>
>The JSON API is in the form…
>
>{
> "action": "subscription.getall",
> "result": "success",
> "nextPage": 3,
> "subscriptions": [
> "khyNPLY3TYiQhBfHyjZINg",
> "w46bQ2-gTayzJfFXbV1VWg",
> "86cFjtgCRlSmOPbLcX_mCA",
> "v0yPCSTLSyarVe9NlNalSA",
> "FWhWZ3b6StyfiJ_GnyHswQ",
> "A5xx9TF_R26PcCMdagAC8Q",
> "pgNumDwTSbaLeNY6FtUF3Q",
> "IK2eD1nvSP-L3yilE6K7BQ",
> "iZ8qUO-MSwuezTn_elPb3g",
> "gLspcP3NRqmdOm615HSTig",
> "HYKxh1JfTcyUhTgJxAxfWg",
> "1RkJixj_QKOg_c7G_wGIZA",
> "LiPzVuKnT2Go1WWteQkZtw",
> "V5wXtLilSsWGkMYSiLVy2g",
> "MseNZ_LBRu63R84k9knIKg"
> ]
>}
That translates to a _record_ which has a list as one of its values.
use AppleScript version "2.4" -- Mac OS 10.10 (Yosemite) or later.
use framework "Foundation"
set jsonURL to "http://your/url/here" -- Set as required.
set jsonURL to current application's class "NSURL"'s URLWithString:(jsonURL)
set jsonData to current application's class "NSData"'s
dataWithContentsOfURL:(jsonURL)
set {jsonObject, theError} to current application's class
"NSJSONSerialization"'s JSONObjectWithData:(jsonData) options:(0)
|error|:(reference)
if (jsonObject is missing value) then error (theError's
localizedDescription() as text)
set aSerialNumber to "w46bQ2-gTayzJfFXbV1VWg" -- Or whatever.
-- There are various ways to check if this occurs in the JSON object's
'subscriptions' array. For instance:
set jsonRecord to jsonObject as record
set serialNumberIsInList to (aSerialNumber is in jsonRecord's subscriptions)
-- Or:
set subscriptionsList to (jsonObject's objectForKey:("subscriptions")) as list
set serialNumberIsInList to (aSerialNumber is in subscriptionsList)
-- Or:
set subscriptionsArray to jsonObject's objectForKey:("subscriptions")
set serialNumberIsInList to (subscriptionsArray's
containsObject:(aSerialNumber)) as boolean
NG
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden