Trouble Grabbing and Parsing JSON Data

I have a Shortcut to do the following:

  • take the contents of the clipboard (usually a single word)
  • query an online dictionary to grab the definitions of the word
  • run JavaScript code on the result of the Get Contents of URL GET request to parse the JSON result for certain values.
  • Display the parsed values.

When I get to the step of running the JS code on the webpage results, I get an error message, “run JavaScript on Active Safari Tab failed because Shortcuts couldn’t convert from Text to Safari web page.” I am not sure why - the result returned from the web page looks like plain text in JSON format. Any suggestions on what I need to do?

I’m not great with javascript, but I think you are using the wrong action. The “Run JavaScript on Active Safari Tab” is expecting a safari web page as the input. You are probably wanting the action “Run JavaScript for Mac Automation.” If your script doesn’t work becuase it is JXA, there are third party apps that can run javascript. Actions app (free), BetterTouchTool, Keyboard Maestro are just a few. Hope that helps!

1 Like

@Tdawg_07 is correct. That action is to run JS in an already open tab in Safari.

You can run JS inside a shortcut (example) but you can probably do it with native actions too.

1 Like

Thank you for pointing that out. I have tried to implement this one as a test, but it doesn’t seem to work for me. What am I doing wrong? I think the part that generates the URL with JavaScript isn’t working properly, or isn’t grabbing my JSON properly.

https://www.icloud.com/shortcuts/5af27147d9e647e7b6a67ffd098b91ff

One more thing when running JS natively: https://www.icloud.com/shortcuts/dc58ab8abe454b54a3b3cb27a41d28cb

More info:

1 Like

Thank you for the responses - very very helpful. I managed to get it working on this shortcut.

https://www.icloud.com/shortcuts/65352eaf567f4fc3847535e0f55fda24

However, I’m trying to take it one step further and grab values what is a very complex nested JSON. I am trying to grab values under the “items” array of “WORD” below. However, some of the values are in nested arrays. I have managed to grab what I want under items, but can’t grab the values under nested.

What I want is:
rank
expEntry
meansCollector.means.value
meansCollector.expAliasEntryAlwaysList.originLanguageValue

JSON here:

{
    "query": "포장",
    "searchResultMap": {
        "searchResultListMap": {
            "EXAMPLE": {

            },
            "OPEN": { 

            },
            "MEANING": {

            },
            "THESAURUS": {

            },
            "WORD": {
                "query": "포장",
                "sectionType": "WORD",
                "items": [
                    {
                        "rank": "1",
                        "meansCollector": [
                            {
                                "partOfSpeech": null,
                                "means": [
                                    {
                                        "exampleTrans": "",
                                        "value": "English meaning here",
                                        "exampleOri": ""
                                    }


                                ],
                                "entryID": "95c86bce395d4156b77da2aa604a7317",
                                "expEntry": "포장",
                                "expAliasEntryAlwaysList": [
                                    {
                                        "originLanguageValue": "鋪裝"
                                    }
                                ]
                            }
                        ]

                    },
                    {
                        "rank": "2",
                        "meansCollector": [
                            {
                                "partOfSpeech": null,
                                "means": [
                                    {
                                        "exampleTrans": "",
                                        "value": "Second English meaning here",
                                        "exampleOri": ""
                                    }


                                ],
                                "entryID": "95c86bce395d4156b77da2aa604a7317",
                                "expEntry": "포장2",
                                "expAliasEntryAlwaysList": [
                                    {
                                        "originLanguageValue": "鋪裝2"
                                    }
                                ]
                            }
                        ]

                    }



                ]

            },
            "VLIVE": {

            }


        }
    }
 }

You need to use nested repeat loops for nested arrays: https://www.icloud.com/shortcuts/8d8871f3516443939fe7c089fed3221d