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?
@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.