jsonSet workaround in script

Hi,

I have a lot of OPC tags, each OPC tags are “Documentation” type with UDT.

Want to update some values of struct member of UDT for these OPC tags through buttons in a template pop out window. I could pass the UDT structure (json format data) to the pop out window template and use jsonGet function to parse the UDT data in the pop out window. I want to use the jsonSet function to set some struct member of UDT data in the popped window but it seems that the jsonSet function doesn’t work in the button event function script.

It seems that we can only set value for a concrete tag. How could we set a dynamic tag through the callback function script of mouse click operation as the jsonSet function doesn’t work in the script?

jsonGet and jsonSet are functions in Ignition’s expression language, they aren’t usable from places where you’re writing scripts in Python.

This case is a little awkward - you’ll have to parse the JSON into a dictionary using jsonDecode, modify the corresponding element (something like foo["documentation"] = "blah", depends on the structure of your UDT), then turn the dictionary back to JSON with jsonEncode, and write that updated JSON back to the tag with system.tag.write.

edit: the Document object in our tag system might be a mutable GSON object and not a String… I’ll have to look at this in a bit when I get the office.

1 Like

@Kevin.Herron

Thanks for your feedback

In fact, I have a lot of OPC tags with vector struct content like {X:12.34, Y:23.45, Z:45.56}.
It has button in main window 1, when clicking the [button] of it in snapshot 1. It will pop out another window as show in snapshot 2.

The displayed content (X,Y,Z of vector tags) in snapshot 2 is passed in the parameter with the Vector customer property, which is linked with the tag VectorTag in window of snapshot 1.

We want to update the struct member X or Y in the popped out window of snapshot 2 with button such as [Increase Vector.X with 1]. As many vector tags will use such pop out window template to update their X, Y member. So how could we handle such case as it seem that we need to update value for a concrete tag in the callback of mouse click operation as shown in the third snapshot for the buttons of snapshot 2?

Is it possible to use dynamic tags in the script of mouse click operation?

Solve the issue by re-organize the path of tag in the script as it’s easy to do such work with python.

Or use below script:

The UDT member ‘param1’ of ‘OPC_tag’ can be updated and writen back to OPCUA server.

value1 = system.tag.read(‘OPC_tag’).value
value1 = system.util.jsonDecode(str(value1))
value1[‘param1’] = ‘updated_param1_value’
value2 = system.util.jsonEncode(value1)
system.tag.write(‘OPC_tag’, value2)