Applescript to read the JSON file

Hi All,

Is it possible to read the JSON file and get the each value and key using applescript? Please advice.

You could try this.

https://www.mousedown.net/software/JSONHelper.html

(It was written by a friend of mine.)

Hi,

Is it possible to read it without third party tool? Any direct way?

Google:
json site:macscripter.net

From a previous reply in AppleScriptObjC forum.

Put the json’s text into variable jsonString

use framework "Foundation"

set jsonList to (current application's NSJSONSerialization's JSONObjectWithData:((current application's NSString's stringWithString:jsonString)'s dataUsingEncoding:(current application's NSUTF8StringEncoding)) options:0 |error|:(missing value)) as record
	

jsonList will contain record of records

Example 1: Retrieving JSON data from a UTF8 encoded JSON file.


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
property NSString : a reference to current application's NSString
property NSJSONSerialization : a reference to current application's NSJSONSerialization
property NSUTF8StringEncoding : a reference to current application's NSUTF8StringEncoding

set jsonFileAlias to (choose file of type "public.json")
set jsonString to (read jsonFileAlias)

set jsonString to NSString's stringWithString:jsonString
set jsonData to jsonString's dataUsingEncoding:NSUTF8StringEncoding
(NSJSONSerialization's JSONObjectWithData:jsonData options:0 |error|:(missing value)) as record

.
Example 2: Retrieving JSON data from a web page.


use framework "Foundation"
use scripting additions
property NSURL : a reference to current application's NSURL
property NSData : a reference to current application's NSData
property NSJSONSerialization : a reference to current application's NSJSONSerialization
property myFreePrivateKey : "43b425795a72994b3b4cd305488a4b2d24008dae"

set theUrl to "https://emoji-api.com/emojis?search=apple&access_key=" & myFreePrivateKey

set jsonData to NSData's dataWithContentsOfURL:(NSURL's URLWithString:theUrl)
set theNSArray to (NSJSONSerialization's JSONObjectWithData:jsonData options:0 |error|:(missing value))

set unicodeNamesAndCharacters to {}
repeat with anItem in (theNSArray as list)
	tell anItem
		set end of unicodeNamesAndCharacters to (its unicodeName & " " & its |character|)
	end tell
end repeat

return unicodeNamesAndCharacters

Hi:

I ran your script to get json data from a test file. My result:

 {book:{{|language|:"C", |id|:"444", edition:"First", author:"Dennis Ritchie"}, {|language|:"C++", |id|:"555", edition:"second", author:" Bjarne Stroustrup", category:"Science Fiction"}}}

How can I get the individual data items with Applescript, such as Book 1’s Language and Author? It doesn’t seem to respond to my normal list commands, like “Get item 2 of item 2 of jsonData”.

Thanks,

k

In the example below jsonRecord is an AppleScript record, not a list.
A record is like a list but each item has a name, not an index number
You get each item by name

Example below


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set jsonRecord to {book:{{language:"C", |id|:"444", edition:"First", author:"Dennis Ritchie"}, {language:"C++", |id|:"555", edition:"second", author:" Bjarne Stroustrup", category:"Science Fiction"}}}
set bookList to book of jsonRecord
set bookAuthor to author of item 1 of bookList
set bookLanguage to language of item 1 of bookList

BTW, A list can contains records, and a record can contain lists
Both can contain any AppleScript data type. i.e strings and numbers

Ahhhh, thanks RobertFern, pretty nifty!