-1

I am trying to make a simple home automation workflow using Automator on OSX.

It starts by getting a JSON from an HTTP request using shell script. The JSON is then modified using JavaScript and posted via another HTTP request via shell script.

The problem I am facing is that specifically on automator, JSON.parse() doesn't seem to parse the received string correctly. Looking only at the JSON.parse() function, this is the input:

"{\"accessories\":[{\"aid\":1,\"services\":[{\"type\":\"3E\",\"iid\":1,\"characteristics\":[{\"type\":\"14\",\"iid\":2,\"perms\":[\"pw\"],\"description\":\"Identify\",\"format\":\"bool\"},{\"type\":\"20\",\"iid\":3,\"value\":\"homebridge.io\",\"perms\":[\"pr\"],\"description\":\"Manufacturer\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"21\",\"iid\":4,\"value\":\"homebridge\",\"perms\":[\"pr\"],\"description\":\"Model\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"23\",\"iid\":5,\"value\":\"Homebridge 7168 8A14\",\"perms\":[\"pr\"],\"description\":\"Name\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"30\",\"iid\":6,\"value\":\"0E:1E:A2:02:71:68\",\"perms\":[\"pr\"],\"description\":\"Serial Number\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"52\",\"iid\":7,\"value\":\"1.6.1\",\"perms\":[\"pr\"],\"description\":\"Firmware Revision\",\"format\":\"string\"}]},{\"type\":\"A2\",\"iid\":2000000008,\"characteristics\":[{\"type\":\"37\",\"iid\":9,\"value\":\"1.1.0\",\"perms\":[\"pr\"],\"description\":\"Version\",\"format\":\"string\",\"maxLen\":64}]}]},{\"aid\":6,\"services\":[{\"type\":\"3E\",\"iid\":1,\"characteristics\":[{\"type\":\"14\",\"iid\":2,\"perms\":[\"pw\"],\"description\":\"Identify\",\"format\":\"bool\"},{\"type\":\"20\",\"iid\":3,\"value\":\"None\",\"perms\":[\"pr\"],\"description\":\"Manufacturer\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"21\",\"iid\":4,\"value\":\"None\",\"perms\":[\"pr\"],\"description\":\"Model\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"23\",\"iid\":5,\"value\":\"ZigBee Permit Join\",\"perms\":[\"pr\"],\"description\":\"Name\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"30\",\"iid\":6,\"value\":\"dstltuakzw\",\"perms\":[\"pr\"],\"description\":\"Serial Number\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"52\",\"iid\":7,\"value\":\"1.0.0\",\"perms\":[\"pr\"],\"description\":\"Firmware Revision\",\"format\":\"string\"}]},{\"type\":\"49\",\"iid\":8,\"characteristics\":[{\"type\":\"25\",\"iid\":9,\"value\":0,\"perms\":[\"ev\",\"pr\",\"pw\"],\"description\":\"On\",\"format\":\"bool\"}]}]},{\"aid\":7,\"services\":[{\"type\":\"3E\",\"iid\":1,\"characteristics\":[{\"type\":\"14\",\"iid\":2,\"perms\":[\"pw\"],\"description\":\"Identify\",\"format\":\"bool\"},{\"type\":\"20\",\"iid\":3,\"value\":\"None\",\"perms\":[\"pr\"],\"description\":\"Manufacturer\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"21\",\"iid\":4,\"value\":\"None\",\"perms\":[\"pr\"],\"description\":\"Model\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"23\",\"iid\":5,\"value\":\"ZigBee Touchlink\",\"perms\":[\"pr\"],\"description\":\"Name\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"30\",\"iid\":6,\"value\":\"kaxqkaj8hj\",\"perms\":[\"pr\"],\"description\":\"Serial Number\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"52\",\"iid\":7,\"value\":\"1.0.0\",\"perms\":[\"pr\"],\"description\":\"Firmware Revision\",\"format\":\"string\"}]},{\"type\":\"49\",\"iid\":8,\"characteristics\":[{\"type\":\"25\",\"iid\":9,\"value\":0,\"perms\":[\"ev\",\"pr\",\"pw\"],\"description\":\"On\",\"format\":\"bool\"}]}]},{\"aid\":8,\"services\":[{\"type\":\"3E\",\"iid\":1,\"characteristics\":[{\"type\":\"14\",\"iid\":2,\"perms\":[\"pw\"],\"description\":\"Identify\",\"format\":\"bool\"},{\"type\":\"20\",\"iid\":3,\"value\":\"Philips\",\"perms\":[\"pr\"],\"description\":\"Manufacturer\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"21\",\"iid\":4,\"value\":\"LLC020\",\"perms\":[\"pr\"],\"description\":\"Model\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"23\",\"iid\":5,\"value\":\"Hue Go\",\"perms\":[\"pr\"],\"description\":\"Name\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"30\",\"iid\":6,\"value\":\"0x00178801047a69e6\",\"perms\":[\"pr\"],\"description\":\"Serial Number\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"52\",\"iid\":7,\"value\":\"2.5.10\",\"perms\":[\"pr\"],\"description\":\"Firmware Revision\",\"format\":\"string\"},{\"type\":\"54\",\"iid\":8,\"value\":\"5.127.1.26420\",\"perms\":[\"pr\"],\"description\":\"Software Revision\",\"format\":\"string\"},{\"type\":\"53\",\"iid\":9,\"value\":\"1\",\"perms\":[\"pr\"],\"description\":\"Hardware Revision\",\"format\":\"string\"}]},{\"type\":\"43\",\"iid\":10,\"characteristics\":[{\"type\":\"CE\",\"iid\":15,\"value\":445,\"perms\":[\"ev\",\"pr\",\"pw\"],\"description\":\"Color Temperature\",\"format\":\"int\",\"minValue\":140,\"maxValue\":500,\"minStep\":1},{\"type\":\"25\",\"iid\":11,\"value\":0,\"perms\":[\"ev\",\"pr\",\"pw\"],\"description\":\"On\",\"format\":\"bool\"},{\"type\":\"13\",\"iid\":12,\"value\":31,\"perms\":[\"ev\",\"pr\",\"pw\"],\"description\":\"Hue\",\"format\":\"float\",\"unit\":\"arcdegrees\",\"minValue\":0,\"maxValue\":360,\"minStep\":1},{\"type\":\"2F\",\"iid\":13,\"value\":82,\"perms\":[\"ev\",\"pr\",\"pw\"],\"description\":\"Saturation\",\"format\":\"float\",\"unit\":\"percentage\",\"minValue\":0,\"maxValue\":100,\"minStep\":1},{\"type\":\"8\",\"iid\":14,\"value\":100,\"perms\":[\"ev\",\"pr\",\"pw\"],\"description\":\"Brightness\",\"format\":\"int\",\"unit\":\"percentage\",\"minValue\":0,\"maxValue\":100,\"minStep\":1}]}]}]}"

This is the input beautified with https://jsonbeautifier.org:

{
  "accessories": [
    {
      "aid": 1,
      "services": [
        {
          "type": "3E",
          "iid": 1,
          "characteristics": [
            {
              "type": "14",
              "iid": 2,
              "perms": [
                "pw"
              ],
              "description": "Identify",
              "format": "bool"
            },
            {
              "type": "20",
              "iid": 3,
              "value": "homebridge.io",
              "perms": [
                "pr"
              ],
              "description": "Manufacturer",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "21",
              "iid": 4,
              "value": "homebridge",
              "perms": [
                "pr"
              ],
              "description": "Model",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "23",
              "iid": 5,
              "value": "Homebridge XXXX XXXX",
              "perms": [
                "pr"
              ],
              "description": "Name",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "30",
              "iid": 6,
              "value": "xx:xx:xx:xx",
              "perms": [
                "pr"
              ],
              "description": "Serial Number",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "52",
              "iid": 7,
              "value": "1.6.1",
              "perms": [
                "pr"
              ],
              "description": "Firmware Revision",
              "format": "string"
            }
          ]
        },
        {
          "type": "A2",
          "iid": 2000000008,
          "characteristics": [
            {
              "type": "37",
              "iid": 9,
              "value": "1.1.0",
              "perms": [
                "pr"
              ],
              "description": "Version",
              "format": "string",
              "maxLen": 64
            }
          ]
        }
      ]
    },
    {
      "aid": 6,
      "services": [
        {
          "type": "3E",
          "iid": 1,
          "characteristics": [
            {
              "type": "14",
              "iid": 2,
              "perms": [
                "pw"
              ],
              "description": "Identify",
              "format": "bool"
            },
            {
              "type": "20",
              "iid": 3,
              "value": "None",
              "perms": [
                "pr"
              ],
              "description": "Manufacturer",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "21",
              "iid": 4,
              "value": "None",
              "perms": [
                "pr"
              ],
              "description": "Model",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "23",
              "iid": 5,
              "value": "ZigBee Permit Join",
              "perms": [
                "pr"
              ],
              "description": "Name",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "30",
              "iid": 6,
              "value": "dstltuakzw",
              "perms": [
                "pr"
              ],
              "description": "Serial Number",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "52",
              "iid": 7,
              "value": "1.0.0",
              "perms": [
                "pr"
              ],
              "description": "Firmware Revision",
              "format": "string"
            }
          ]
        },
        {
          "type": "49",
          "iid": 8,
          "characteristics": [
            {
              "type": "25",
              "iid": 9,
              "value": 0,
              "perms": [
                "ev",
                "pr",
                "pw"
              ],
              "description": "On",
              "format": "bool"
            }
          ]
        }
      ]
    },
    {
      "aid": 7,
      "services": [
        {
          "type": "3E",
          "iid": 1,
          "characteristics": [
            {
              "type": "14",
              "iid": 2,
              "perms": [
                "pw"
              ],
              "description": "Identify",
              "format": "bool"
            },
            {
              "type": "20",
              "iid": 3,
              "value": "None",
              "perms": [
                "pr"
              ],
              "description": "Manufacturer",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "21",
              "iid": 4,
              "value": "None",
              "perms": [
                "pr"
              ],
              "description": "Model",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "23",
              "iid": 5,
              "value": "ZigBee Touchlink",
              "perms": [
                "pr"
              ],
              "description": "Name",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "30",
              "iid": 6,
              "value": "kaxqkaj8hj",
              "perms": [
                "pr"
              ],
              "description": "Serial Number",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "52",
              "iid": 7,
              "value": "1.0.0",
              "perms": [
                "pr"
              ],
              "description": "Firmware Revision",
              "format": "string"
            }
          ]
        },
        {
          "type": "49",
          "iid": 8,
          "characteristics": [
            {
              "type": "25",
              "iid": 9,
              "value": 0,
              "perms": [
                "ev",
                "pr",
                "pw"
              ],
              "description": "On",
              "format": "bool"
            }
          ]
        }
      ]
    },
    {
      "aid": 8,
      "services": [
        {
          "type": "3E",
          "iid": 1,
          "characteristics": [
            {
              "type": "14",
              "iid": 2,
              "perms": [
                "pw"
              ],
              "description": "Identify",
              "format": "bool"
            },
            {
              "type": "20",
              "iid": 3,
              "value": "Philips",
              "perms": [
                "pr"
              ],
              "description": "Manufacturer",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "21",
              "iid": 4,
              "value": "LLC020",
              "perms": [
                "pr"
              ],
              "description": "Model",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "23",
              "iid": 5,
              "value": "Hue Go",
              "perms": [
                "pr"
              ],
              "description": "Name",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "30",
              "iid": 6,
              "value": "0x00178801047a69e6",
              "perms": [
                "pr"
              ],
              "description": "Serial Number",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "52",
              "iid": 7,
              "value": "2.5.10",
              "perms": [
                "pr"
              ],
              "description": "Firmware Revision",
              "format": "string"
            },
            {
              "type": "54",
              "iid": 8,
              "value": "5.127.1.26420",
              "perms": [
                "pr"
              ],
              "description": "Software Revision",
              "format": "string"
            },
            {
              "type": "53",
              "iid": 9,
              "value": "1",
              "perms": [
                "pr"
              ],
              "description": "Hardware Revision",
              "format": "string"
            }
          ]
        },
        {
          "type": "43",
          "iid": 10,
          "characteristics": [
            {
              "type": "CE",
              "iid": 15,
              "value": 500,
              "perms": [
                "ev",
                "pr",
                "pw"
              ],
              "description": "Color Temperature",
              "format": "int",
              "minValue": 140,
              "maxValue": 500,
              "minStep": 1
            },
            {
              "type": "25",
              "iid": 11,
              "value": 1,
              "perms": [
                "ev",
                "pr",
                "pw"
              ],
              "description": "On",
              "format": "bool"
            },
            {
              "type": "13",
              "iid": 12,
              "value": 270,
              "perms": [
                "ev",
                "pr",
                "pw"
              ],
              "description": "Hue",
              "format": "float",
              "unit": "arcdegrees",
              "minValue": 0,
              "maxValue": 360,
              "minStep": 1
            },
            {
              "type": "2F",
              "iid": 13,
              "value": 100,
              "perms": [
                "ev",
                "pr",
                "pw"
              ],
              "description": "Saturation",
              "format": "float",
              "unit": "percentage",
              "minValue": 0,
              "maxValue": 100,
              "minStep": 1
            },
            {
              "type": "8",
              "iid": 14,
              "value": 10,
              "perms": [
                "ev",
                "pr",
                "pw"
              ],
              "description": "Brightness",
              "format": "int",
              "unit": "percentage",
              "minValue": 0,
              "maxValue": 100,
              "minStep": 1
            }
          ]
        }
      ]
    }
  ]
}

And this is the output:

{{accessories:{{aid:1, services:{{type:"3E", iid:1, characteristics:{{type:"14", iid:2, perms:{"pw"}, description:"Identify", format:"bool"}, {type:"20", iid:3, |value|:"homebridge.io", perms:{"pr"}, description:"Manufacturer", format:"string", maxLen:64}, {type:"21", iid:4, |value|:"homebridge", perms:{"pr"}, description:"Model", format:"string", maxLen:64}, {type:"23", iid:5, |value|:"Homebridge XXX XXXX", perms:{"pr"}, description:"Name", format:"string", maxLen:64}, {type:"30", iid:6, |value|:"xx:xx:xx:xx", perms:{"pr"}, description:"Serial Number", format:"string", maxLen:64}, {type:"52", iid:7, |value|:"1.6.1", perms:{"pr"}, description:"Firmware Revision", format:"string"}}}, {type:"A2", iid:2.000000008E+9, characteristics:{{type:"37", iid:9, |value|:"1.1.0", perms:{"pr"}, description:"Version", format:"string", maxLen:64}}}}}, {aid:6, services:{{type:"3E", iid:1, characteristics:{{type:"14", iid:2, perms:{"pw"}, description:"Identify", format:"bool"}, {type:"20", iid:3, |value|:"None", perms:{"pr"}, description:"Manufacturer", format:"string", maxLen:64}, {type:"21", iid:4, |value|:"None", perms:{"pr"}, description:"Model", format:"string", maxLen:64}, {type:"23", iid:5, |value|:"ZigBee Permit Join", perms:{"pr"}, description:"Name", format:"string", maxLen:64}, {type:"30", iid:6, |value|:"dstltuakzw", perms:{"pr"}, description:"Serial Number", format:"string", maxLen:64}, {type:"52", iid:7, |value|:"1.0.0", perms:{"pr"}, description:"Firmware Revision", format:"string"}}}, {type:"49", iid:8, characteristics:{{type:"25", iid:9, |value|:0, perms:{"ev", "pr", "pw"}, description:"On", format:"bool"}}}}}, {aid:7, services:{{type:"3E", iid:1, characteristics:{{type:"14", iid:2, perms:{"pw"}, description:"Identify", format:"bool"}, {type:"20", iid:3, |value|:"None", perms:{"pr"}, description:"Manufacturer", format:"string", maxLen:64}, {type:"21", iid:4, |value|:"None", perms:{"pr"}, description:"Model", format:"string", maxLen:64}, {type:"23", iid:5, |value|:"ZigBee Touchlink", perms:{"pr"}, description:"Name", format:"string", maxLen:64}, {type:"30", iid:6, |value|:"kaxqkaj8hj", perms:{"pr"}, description:"Serial Number", format:"string", maxLen:64}, {type:"52", iid:7, |value|:"1.0.0", perms:{"pr"}, description:"Firmware Revision", format:"string"}}}, {type:"49", iid:8, characteristics:{{type:"25", iid:9, |value|:0, perms:{"ev", "pr", "pw"}, description:"On", format:"bool"}}}}}, {aid:8, services:{{type:"3E", iid:1, characteristics:{{type:"14", iid:2, perms:{"pw"}, description:"Identify", format:"bool"}, {type:"20", iid:3, |value|:"Philips", perms:{"pr"}, description:"Manufacturer", format:"string", maxLen:64}, {type:"21", iid:4, |value|:"LLC020", perms:{"pr"}, description:"Model", format:"string", maxLen:64}, {type:"23", iid:5, |value|:"Hue Go", perms:{"pr"}, description:"Name", format:"string", maxLen:64}, {type:"30", iid:6, |value|:"0x00178801047a69e6", perms:{"pr"}, description:"Serial Number", format:"string", maxLen:64}, {type:"52", iid:7, |value|:"2.5.10", perms:{"pr"}, description:"Firmware Revision", format:"string"}, {type:"54", iid:8, |value|:"5.127.1.26420", perms:{"pr"}, description:"Software Revision", format:"string"}, {type:"53", iid:9, |value|:"1", perms:{"pr"}, description:"Hardware Revision", format:"string"}}}, {type:"43", iid:10, characteristics:{{type:"CE", iid:15, |value|:500, perms:{"ev", "pr", "pw"}, description:"Color Temperature", format:"int", minValue:140, maxValue:500, minStep:1}, {type:"25", iid:11, |value|:1, perms:{"ev", "pr", "pw"}, description:"On", format:"bool"}, {type:"13", iid:12, |value|:270, perms:{"ev", "pr", "pw"}, description:"Hue", format:"float", unit:"arcdegrees", minValue:0, maxValue:360, minStep:1}, {type:"2F", iid:13, |value|:100, perms:{"ev", "pr", "pw"}, description:"Saturation", format:"float", unit:"percentage", minValue:0, maxValue:100, minStep:1}, {type:"8", iid:14, |value|:10, perms:{"ev", "pr", "pw"}, description:"Brightness", format:"int", unit:"percentage", minValue:0, maxValue:100, minStep:1}}}}}}}}

Sorry, I couldn't beautify it due to the syntax error. You will see that the value property is parsed to |value| rather than "value"

The input is a string, not a JSON object. The output should be a JSON object but the string is not parsed correctly and results in a "undefined" object. The whole code works flawlessly in JavaScript, but in Automator the JSON.parse() method fails.

Has anyone encountered and hopefully solved this issue?

8
  • This is the input: which is valid JSON And this is the output: which seems to have no [ or ] ... in fact, [] have been changed to {} .... what did you do to process the input? Commented Aug 15, 2023 at 22:30
  • This isn't JSON. It's an object. Thus you won't be able to use json.parse() on it. Commented Aug 15, 2023 at 22:30
  • The JSON is then modified ... you should first parse the JSON to an object, modify the object ... don't modify JSON unless you really want pain and anguish Commented Aug 15, 2023 at 22:32
  • 1
    The use of property labels and the lack of square brackets would seem to indicate that the result is being coerced to an AppleScript record. AppleScript records and lists both use the curly {} brackets (although square [] brackets can be accepted for lists), and pipes || are used as an escape for labels that would normally not be allowed, since they are not strings. Commented Aug 16, 2023 at 15:15
  • 1
    The object is returned that way in the log, but it appears to work as expected in the current Run JavaScript action and when passed to another Run JavaScript action. Just use strings for the key names (no pipes), and don’t forget that input is an array. Commented Aug 16, 2023 at 21:48

1 Answer 1

0

@red_menace's comment is the answer. The JSON is parsed properly, but Automator prints it coerces into an AppleScript register syntax, which give the impression that is was not parsed properly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.