Return alarm status via web dev module

(I couldn’t find a question like this one even though it’s hard to imagine it hadn’t been already asked).

I’d like to respond with the output of alarm.queryStatus when a request with a GET verb is made.

But, the script

    alarmDict = system.alarm.queryStatus(source=["*HiAlarm*"])
jsonString = system.util.jsonEncode(alarmDict)
return {'html': '<html><body>" + jsonString + "</body></html>'}

produces

Traceback (most recent call last):

File “<project32/alarms:doGet>”, line 4, in doGet
TypeError: cannot concatenate ‘str’ and ‘NoneType’ objects

Is there a better approach?

Assuming alarmDict (and therefore jsonString) are valid variables, if you directly copy and pasted your code you’re using incompatible string delimiters:

return {‘html’: [size=200]’[/size][size=200]"[/size] + jsonString + [size=200]"[/size][size=200]’[/size]}

Try matching up the single quotes and double quotes:
return {‘html’: [size=200]’[/size][size=200]’[/size] + jsonString + [size=200]’[/size][size=200]’[/size]}

You could also use system.util.getLogger() to print those variables to the gateway console to ensure they’re valid.

The problem here is that the jsonEncode() function does not seem to accept the “list” object produced by the queryStatus() function, so the jsonString variable never points to anything other than a NoneType. We can convert this to a Python list of alarm event objects using syntax like:

alarmDict = [x for x in system.alarm.queryStatus(source=[“HiAlarm”])]

However, a list of objects like this will cause the jsonEncode function to reach max recursion depth very quickly.

I think that the easiest solution is to convert this data to a PyDataset before encoding to Json. This would look something like this:

alarmDict = system.dataset.toPyDataSet(system.alarm.queryStatus(source=[“HiAlarm”]).getDataset())
jsonString = system.util.jsonEncode(alarmDict)
return {“response”:jsonString, “contentType”:“application/json”}

I currently am trying to use system.util.jsonEncode() for a large dict and getting the max recursion depth error.
What can i do to prevent this? I need the large dict size for doing http POST requests.