Create JSON from tags

We're in love with Ignition! Great work,

However, we miss a straightforward way to create JSON from OPC/memory tags.
[Where: key = tag_name and value = value]

We use JSON to connect to our MES. A flat UDT is used, which we'll iterate over.

Presumably, you have the names (as tagpaths) in list form. It is a one-liner to get the corresponding values. It is another one-liner to make a key-value dictionary. Like so:

tagValues = [qv.value for qv in system.readBlocking(tagPaths)]
tagDict = dict(zip([path.split('/')[-1] for path in tagPaths], tagValues)

From there, it is one more to use the system.util.jsonEncode() function. Though the latter can be omitted if you submit the dictionary as a json argument to httpClient.

3 Likes

It's not every day you get to fix something from Phil !

tagValues = [qv.value for qv in system.tag.readBlocking(tagPaths)]
tagDict = dict(zip([path.split('/')[-1] for path in tagPaths], tagValues))

Fixed call to system.tag.readBlocking and missing parenthesis at the end.

4 Likes

Just testing the audience... * cough *

3 Likes

You can also right click on a tag or folder and select "Copy JSON" and paste that into a text editor.

Export works the same way.

Depending on your situation either method may be ideal.

2 posts were merged into an existing topic: Create JSON Message format from tags