fix a string

set a to "zip -r foo . -i \*.c"

gives error

How to handle the above problem?

Try \* instead of *

I am creating json file from paragraphs of text file. The above string is one such para from the text file. Could someone fix the problem here:
https://github.com/mgax/applescript-json

Then try this (requires Yosemite or later):

use scripting additions
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 saveTo:posixPath
	--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
	if posixPath is missing value then -- return string
		-- 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
	else
		-- write data to file
		theData's writeToFile:posixPath atomically:true
		return result as boolean -- returns false if save failed
	end if
end convertASToJSON:saveTo:

Hello Shane

It’s an old thread but I retrieved your handler while making some cleaning.
Trying to use it I got errors.

I tried with :

set theString to "just for try"
my convertASToJSON:theString saveTo:(missing value)

and got error “*** +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write” number -10000

I made an other attempt with:

set theString to "just for try"
set theString to current application's NSString's stringWithString:theString
my convertASToJSON:theString saveTo:(missing value)

and got the same error.

What am I doing wrongly ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 22 octobre 2019 18:18:21

it seems, even with single string, it will be provided to handler into the list braces:

{“just for try”}

Thanks.
I really didn’t guess that it was that.

set theString to "just for try"
set value1 to my convertASToJSON:{theString} saveTo:(missing value)

set theList to {"other try", "31/12/1943", 3.14159}
set value2 to my convertASToJSON:theList saveTo:(missing value)

log value1 & linefeed & value2
(*["just for try"]
["other try","31\/12\/1943",3.1415899999999999]*)

value1 & linefeed & value2
(*
"[\"just for try\"]
[\"other try\",\"31\\/12\\/1943\",3.1415899999999999]"
*)

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 22 octobre 2019 20:05:54

Yvan,

JSON is defined as a way of serializing attribute–value pairs (records, basically) and arrays, so you must pass one or the other.

Thanks Shane. It make sense.

I was testing according to what was written in the script:

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

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 23 octobre 2019 11:01:39