JSON Encoding Problems

I’m having JSON encoding problems with the system.util.jsonDecode function.
I am using the system.net.httpGet and getting a JSON string back. Then when I try to change that string to the JSON object, it gives me JSON exception at the colons of a timestamp column.

Sample JSON:
[{'CSTTimeStamp': u'2021-08-25 08:00:00', 'Meter24HFlow': 0.0, 'AssetName': u'4 in TPLT WW', 'FMRate': None, 'FMTotal': 1109954.75, 'MeterReadingStart': 233467.66, 'UnitID': u'2703932_2', 'Meter1HFlow': 0.0, 'ID': 70174099L},{'CSTTimeStamp': u'2021-08-25 07:00:00', 'Meter24HFlow': 0.0, 'AssetName': u'4" TPLT WW', 'FMRate': None, 'FMTotal': 1109954.75, 'MeterReadingStart': 233467.66, 'UnitID': u'2703932_2', 'Meter1HFlow': 0.0, 'ID': 70165961L}]

Any ideas of why it is throwing an exception at the colon of the timestamp?

What object are you printing to get that JSON output? What is the result of print type(obj) for that object?

the type back from the httpGet is unicode.

Ok, but the sample you provided has the Python u prefixes on the strings, so it means you obtained it by printing some object like a list or dictionary and not a raw string value.

Unless the actual string value returned by the httpGet contained those… are you calling another Ignition gateway or something?

I’m using the WebDev module on a 8.1.1 (production) Ignition gateway and 8.1.4 (dev) Ignition gateway.
I’m testing it using the system.net.httpGet function.

I think your WebDev endpoint is not returning valid JSON then. Instead, it’s returning a string representation of some list containing a dictionary.

That is right. In the WebDev function I am getting data from a SQL query, which returns a DataSet. Then I convert that into an array of dictionaries.

I was returning that, but now I removed my return statement and replaced it with this below:

writer = request["servletResponse"].getWriter()
request["servletResponse"].setContentType("application/json")
request["servletResponse"].setCharacterEncoding("utf-8")
writer.write("[")
for i in range(len(jsonObjectToReturn)):
if i == len(jsonObjectToReturn) - 1:
	writer.write(str(jsonObjectToReturn[i]))
else:
	writer.write(str(jsonObjectToReturn[i]) + ",")
writer.write("]")

This is in hopes of figuring out if a memory leak that I have in Ignition (production) is caused by the WebDev endpoint.
I delete every variable at the end of my function which has helped the memory growth from growing so rapidly.
So what Travis Cox had suggested was to put the return data into the request[“servletResponse”] and don’t use a return statement at all.
This works but trying to encode it on after the return from system.net.httpGet is giving me problems.

Calling str on a dictionary doesn’t give you valid JSON. You have to encode it properly; try system.util.jsonEncode.

edit: oops, what problem did you have with that?

Where would I put this?
Like this?

request["servletResponse"] = system.util.jsonEncode(request["servletResponse"])

No, something like this:

writer = request["servletResponse"].getWriter()
request["servletResponse"].setContentType("application/json")
request["servletResponse"].setCharacterEncoding("utf-8")

writer.write(system.util.jsonEncode(jsonObjectToReturn))

Awesome. Thank you so much, that did the trick. Now all the values for each key don’t have the ‘u’ in front. The key column does, any suggestions on how to get that removed? Or should it not really matter?

does it decode now?, then it doesnt matter xd