Help picking item from list of dictionaries

I want to create a shortcut that checks a web API (GitLab) for issues assigned to me, let’s me pick one, and then does a few things from that selection, like start a Toggl timer, set my Slack status, and opens the website for that issue.

I’m currently stuck at a fairly basic early step and am hoping someone here can give me guidance:

The API returns a list of dictionaries, each with different keys in there that I’ll need, like name, issue number and a URL. How do I pick an item from that list? If I just pass that list to a “Choose from List” option, they all just say “Dictionary”. If I first iterate over the list and extract the name, then they say the proper name, but how do I them get the related dictionary, so that I can get the ID and URL? “Choose from List” just returns the name as the output, but not the index.

Am I missing something? Or do I need to again iterate over all items in the list and then check each for the name? I was hoping there’s a simpler way.

Any help or examples appreciated!

Is it possible for you to post sample text of the API response? That would help with solution recommendations!

Try this.

I think it should give you an idea about how to do it.

2 Likes

Or just that :point_up:

Great, thanks.

Though the issue that I have is that the API returns something like this:

[
	{
	  "title": "Switch to better Markdown library",
	  "updated_at":"2018-11-26T20:54:38.689Z",
	  "web_url":"https:\/\/gitlab.com\/myproject\/issues\/97",
	  "time_stats":{"total_time_spent":22500,"time_estimate":0,"human_time_estimate":null,"human_total_time_spent":"6h 15m"},
	  "created_at":"2018-11-21T18:48:46.931Z",
	  "iid":97
	}, ...
]

And if I pass that to “Choose from List”, I don’t get good names, just “dictionary”. So that means I need to turn the list from the API into a dictionary of “title” to “sub-dictionary”, right? I’ll give that a shot!

Any idea how to do that? If I use “Repeat with Each” and do a “Set Dictionary Value” with key “item.title” and value “item”, I end up with a list of dictionaries with just a single key in each, but I need just one dictionary with the titles as keys.

Expressed in JSON, I now get:

[
	{
	  "Title 1": {
		  "title": "Title 1",
		  ...,
	  }
	}, {
		"Title 2": {
		  "title": "Title 2",
		  ...,
	  }, 
	}, ...
]

But I need:

{
  "Title 1": {
	  "title": "Title 1",
	  ...
  }, 
  "Title 2": {
	  "title": "Title 2",
	  ...
  }, ...
}

Try this worked example based on your example data above.

1 Like

Ah, clever solution, to just build the JSON yourself like that. Works well! Thank you.

Only minor issue with it is that it doesn’t maintain the order, but that’s expected here and I can live with that.

JSON and dictionaries are not ordered data sets. They are just grouped key value pairs at the end of the day.

If you want a useful, consistent order for easier user interation, maybe consider a sort using the file filter action like this to get alphabetical order.

Yeah, that makes sense. Though what I get from the API is ordered at first, but turning it into a dictionary then removes the ordering information. I have seen JSON parsers that do maintain the order and then use an ordered dictionary, but I guess that would be a feature request radar for the Shortcuts team :slight_smile:

I ended up with my original approach for now as I got used to the order of the issues on the website. Just needs a clumsy second iteration to find again the selected item. Not ideal, but works.

For posterity, here’s my shortcut to fetch GitLab issues if anyone else wants to use it.

Thanks again for the help!

Next up: Start a Toggl timer for that issue, and when stopping it again, post that duration back to the relevant GitLab issue.