AttributeError: 'com.inductiveautomation.ignition.designer.gui.tool' object has no attribute 'loads'

I am trying to run the following code on the Script Console:

def removeDuplicateKeys():
	import json
	x = '{"name":"Lucca"}'
	y = json.loads(x)
	return y
	
removeDuplicateKeys()

However, I get an error saying the following:

Traceback (most recent call last):
File "", line 7, in
File "", line 4, in removeDuplicateKeys
AttributeError: 'com.inductiveautomation.ignition.designer.gui.tool' object has no attribute 'loads'

I noticed that when i run the same exact code on a different project on the same gateway, it is able to execute properly. Any ideas?

move the import outside of the def. That doesn't really explain why you would be getting this error. There seems to be some type of scope leakage going on, the error is obviously referring to the json.loads() function, so probably has to do with the import not occurring properly.

It's also just best practice to do imports outside of any defs

Do you have a project script library named json?

I tried that. Unfortunately, it did not fix it.

yes! is there a way to call an absolute path to the python library? that way i dont need to refactor everything that is using the local json library?

Not that I'm aware of. Why not use system.util.jsonDecode instead of Jython's json builtin library?

the jsonDecode does not handle well situations with a duplicate key. So i am using the json functions to remove duplicates.

I assume that in your script library you are importing the json builtin library?

If so you can try adding a loads function in that library which will simply delegate the call to the json builtin.

Something like:

def loads(jsonString):
    return json.loads(jsonString)

Though honestly you should probably just rename the library and get the refactoring out of the way.

Avoid using any jython/python builtin names in the future.

2 Likes