iOS Shortcuts: Get Dictionary Value and working with JSON

…or ‘two hours of my life I’m not getting back as a result of a daft mistake’ :).

I’ve just spent a frustrating / puzzling afternoon trying unsuccessfully to parse a JSON dictionary from my own API, using the ‘Get Dictionary Value’ action in Shortcuts. From what I can see, the documentation makes the normally safe assumption that you are working with an API that sets the appropriate content-type for JSON.

I wasn’t, because I’m using Flask, which defaults to text/html. The API endpoints I’m using are a couple of years old and, up until today, I’ve been parsing the JSON using both JavaScript and Objective-C, neither of which are precious about the mime type. Hence the misconfiguration was at the bottom of my list of things to think about.

Back to the shortcut action, the dictionary parsing was just failing silently. I went around the houses on the problem, and when I was working my way through a tutorial that used an external API, I noticed that the JSON result that I got back from hitting the ‘Play’ button for the URL content rendered in a different type of UI element. Your mileage may vary on the appearance, but in dark mode (iOS 14), it renders as a black box. ‘Text/html’ renders as what looks like a webview, on a white background – which makes perfect retrospective sense, as it’s expecting it to be browser content.

Having figured this out, I then jumped on another merry-go-round, trying to avoid ‘double parsing’ – escaped quotes – on a dictionary constructed from variables with the jsonify() method. The data has actually a pretty torturous path before it’s converted to JSON – Flask app route calls an external python script which itself shells out – so I gave up on trying to do it ‘right’. As I need to serve html as well as JSON from the same server instance, I went with this option.