Working with hierarchy of AppleScript list before writing into JSON

Hello, new Script Debugger user here, very happy with my purchase. Though I’m now making some good early progress with learning AppleScript (SD makes it so much easier), I’ve become stuck on a point that I hope someone can help me with.

My objective is to create a data structure in an AS list, populated from AS variables, ​that can be parsed into JSON using the “JSON Helper” tool (I’m aware that NSJSONSerialization is also an option but JSON Helper keeps things simple for me for now).

More specifically, the data structure is that of the Alfred script filter function (which uses JSON). If I can get this working, I’ll be populating it with all sorts of data for different script filters, read via AS from various databases (DEVONthink, Bookends, and so on, as well as my own lists of macros and commands). So, I am trying to create a generic template to that end.

The required JSON structure is this:

{
   "items":[
      {
         "uid":"1234",
         "title":"First Title",
         "subtitle":"First Subtitle",
         "arg":"firstarg",
         "icon":{
            "type":"filetype",
            "path":"~/first.png"
         }
      },
      {
         "uid":"5678",
         "title":"First Subtitle",
         "subtitle":"Second Subtitle",
         "arg":"secondarg",
         "icon":{
            "type":"fileicon",
            "path":"~/Desktop"
         }
      }
   ]
}

Reading this into AS via JSON Helper’s read JSON from command results in the following list:

{|items|:{{title:"First Title", uid:"1234", subtitle:"First Subtitle", arg:"firstarg", icon:{|type|:"filetype", |path|:"~/first.png"}}, {title:"First Subtitle", uid:"5678", subtitle:"Second Subtitle", arg:"secondarg", icon:{|type|:"fileicon", |path|:"~/Desktop"}}}}

This same list can then be read out into JSON again via make JSON from.

The question, then, is how to reconstruct that list structure in AS, populating the fields via variables extracted from whatever database I am dealing with. My assumption was that this would need to be done via a repeat with statement. That is, following the logic: ‘for each record of given database construct an AS list for that record and append to a combined list via set end of.’

The problem therefore concerns the the specific hierarchy of Alfred’s JSON: I don’t know how to place my combined list within the |items| list, as per the above.

This is as far as I was able to get with my testing:

-- make blank list
set theList to {}

-- set vars to populate fields
set theTitle to "a"
set theSubtitle to "b"
set theUID to "c"
set theArg to "d"
set iconType to "e"
set iconPath to "f"

-- create list
set end of theList to {title:theTitle, subtitle:theSubtitle, uid:theUID, arg:theArg, icon:{|type|:iconType, |path|:iconPath}}

-- (just repeating the same statement to simulate a repeat loop)
set end of theList to {title:theTitle, subtitle:theSubtitle, uid:theUID, arg:theArg, icon:{|type|:iconType, |path|:iconPath}}

-- create JSON from combined list
tell application "JSON Helper"
	set theNewJSON to make JSON from theList
end tell

This successfully produces:

[
  {
    "title": "a",
    "uid": "c",
    "subtitle": "b",
    "arg": "d",
    "icon": {
      "type": "e",
      "path": "f"
    }
  },
  {
    "title": "a",
    "uid": "c",
    "subtitle": "b",
    "arg": "d",
    "icon": {
      "type": "e",
      "path": "f"
    }
  }
]

However, that all needs to be placed under |items| in the list hierarchy.

Of course, the simple solution would be to just add the necessary syntax to the resulting string via concatenation. However, I’d prefer to avoid that approach (since it is essentially a hack resulting from my own lack of knowledge).

What I need to understand (and have failed to discover through my searching) is how to work with the hierarchy of the AS list itself. If I can remedy that, I should be able to make the correct JSON without any further modifications.

I would really appreciate any help (even if it’s 'go and read this!…).

You have to create a record before passing it to the JSON helper.

Just before you call JSON Helper place the following:

    set theRecord to {|items|:theList}

and then of course change your call within JSON Helper application to:

set theNewJSON to make JSON from theRecord

Ah, so simple! I really should have figured that out but I guess I only had so much brain capacity to spare that day. Thanks so much.