Amazon s3 aws - parse result??

I’m writing an applescript that automates uploading large files to my Amazon s3 storage in smaller parts via AWS command lines. (It’s more robust in the event of lost internet connection etc). I’m stuck on parsing out a result that comes back from the server. The initiate upload command returns this via “do shell script” (or Terminal):

“{
"UploadId": "gzxqKMTrooMzKfR1D5INKU4gYlI4l_DIAQimziR.wxa9C6Wxm2bC5z0poQXqocuxVk9B6Z0ALkySXLlba6afNA–",
"Key": "files/shush.mp4",
"AbortRuleId": "Incomplete Multipart Uploads",
"AbortDate": "Thu, 23 Feb 2017 00:00:00 GMT",
"Bucket": "XXXXXX"
}”

The result is always in the same format. The text I need is the UploadID, which is the long string starting “gzxqKM…”. I’ve tried this:


set myresult to "{
    \"UploadId\": \"gzxqKMTrooMzKfR1D5INKU4gYlI4l_DIAQimziR.wxa9C6Wxm2bC5z0poQXqocuxVk9B6Z0ALkySXLlba6afNA--\", 
    \"Key\": \"files/shush.mp4\", 
    \"AbortRuleId\": \"Incomplete Multipart Uploads\", 
    \"AbortDate\": \"Thu, 23 Feb 2017 00:00:00 GMT\", 
    \"Bucket\": \"XXXXXX\"
}"

get word 2 of paragraph 2 of myresult

which yields: “gzxqKMTrooMzKfR1D5INKU4gYlI4l_DIAQimziR.wxa9C6Wxm2bC5z0poQXqocuxVk9B6Z0ALkySXLlba6afNA”

It’s missing the last two dashes on the end of the string!! I guess dashes are treated as word delimiters?? Often there are equal signs “=” in the string too, which also causes error. How do I faithfully extract the string?

OK, got it…


--SPLIT
to split(someText, delimiter)
	set AppleScript's text item delimiters to delimiter
	set someText to someText's text items
	set AppleScript's text item delimiters to {""} --> restore delimiters to default value
	return someText
end split

set myresult to "{
    \"UploadId\": \"gzxqKMTrooMzKfR1D5INKU4gYlI4l_DIAQimziR.wxa9C6Wxm2bC5z0poQXqocuxVk9B6Z0ALkySXLlba6afNA--\", 
    \"Key\": \"files/shush.mp4\", 
    \"AbortRuleId\": \"Incomplete Multipart Uploads\", 
    \"AbortDate\": \"Thu, 23 Feb 2017 00:00:00 GMT\", 
    \"Bucket\": \"XXXXXX\"
}"

get item 4 of my split(myresult, "\"")

Hi,

the text is JSON.

With help from AppleScriptObjC you can easily deserialize the JSON

use framework "Foundation"

set myresult to "{
\"UploadId\": \"gzxqKMTrooMzKfR1D5INKU4gYlI4l_DIAQimziR.wxa9C6Wxm2bC5z0poQXqocuxVk9B6Z0ALkySXLlba6afNA--\", 
\"Key\": \"files/shush.mp4\", 
\"AbortRuleId\": \"Incomplete Multipart Uploads\", 
\"AbortDate\": \"Thu, 23 Feb 2017 00:00:00 GMT\", 
\"Bucket\": \"XXXXXX\"
}"

set nsMyResult to current application's NSString's stringWithString:myresult
set theData to nsMyResult's dataUsingEncoding:(current application's NSUTF8StringEncoding)
set {theDictionary, theError} to current application's NSJSONSerialization's JSONObjectWithData:theData options:0 |error|:(reference)
if theDictionary is missing value then error (theError's localizedDescription() as text) number -10000
set uploadID to (theDictionary's objectForKey:"UploadId") as text

Great!! Thanks a lot