Mass conversion of direct OPC tags to UDT's

I have a ton of existing direct OPC tags that need to be converted to UDT's to match standards of current programming. In the end, I just need the original OPC tag path to end up as a parameter of the updated UDT.

Is there any quick way to do this? Originally, I was thinking possible with Find/Replace in a json export, but where the parameters of both span multiple lines and have different properties, I'm not sure that is possible. Would some sort of script work to browse tags and change the type?

jq is a very powerful (somewhat hard to use) tool to make structural manipulations to JSON data:
https://stedolan.github.io/jq/

You could likely also accomplish this through scripting, but I'd probably do it with jq, to be honest.

For example, adding a computed tooltip to all your OPC tags:

Thank you. I will look into this and see if I can work it out.

You can say that again! While it's pretty cool when you finally get the statement right, I could have written something in python, welcomed the next new year, and celebrated the following Christmas in the time it takes to work it out :smile:

If you're familiar with batch or powershell though it might be more logical. I struggle with these as well as I just havebt had the chance or desire to learn them properly

1 Like

Yeah. I'm struggling at the moment just figuring how to direct the program to the file I need to work on etc. I have stuff installed. Can't even get to where I need to come up with the syntax of replacement I need. Just trying to do a sample still.

@PGriffith I've been trying with the sample json string from your other linked forum note. Here's some snips of my setup. I'm missing like a quote or how I'm supposed to reference the file directory or something dumb I'm sure.
image

You probably want this one:

But wrap your jq "query" in single quotes:
jq.exe 'walk xxx' path_to_first_file.json > path_to_output.json

Getting closer at least. It did create file this time, however it is empty.


Looks like some funky quotes snuck into what you're trying. Try recopying, or put it into a text editor and ensuring those are true " characters and there's no other funky stuff.

I used to use this
https://jqplay.org/
Depends how large your file is I guess. It's far easier to work in though

Tried that link and also tried simplifying the query. Not sure what it means by expecting $end

On the website, you don't need to wrap in quotes at the outer layer.

1 Like

Man. I feel like a child trying to work through this. Thank you for your help and patience. I go it to work on the website.

However, when trying to apply it in command shell, it seems to think I have an unterminate 'if' statement.

1 Like

If it's any help, this is my end goal with a sample piece of json export.

I've highlighted the piece of addressing that would need to be retained. If anyone wants to put together the "magic sauce" for Command Shell to make this happen, it would make my weekend. I just don't have as much time to learn a 15th programming language/software lol.

here's the raw json string { "name": "FNR", "tagType": "Folder", "tags": [ { "valueSource": "opc", "opcItemPath": "[PLC_Test]HS9999", "dataType": "Boolean", "name": "Service", "tagType": "AtomicTag", "opcServer": "Ignition OPC-UA Server" }, { "name": "Service2", "typeId": "Logix/ControlBit", "parameters": { "ParentPath": { "dataType": "String", "value": "[PLC_Test]HS9999" } }, "tagType": "UdtInstance", "tags": [ { "name": "AccessLevel", "tagType": "AtomicTag" }, { "name": "LogRequired", "tagType": "AtomicTag" }, { "name": "Output", "tagType": "AtomicTag" }, { "name": "_rev", "tagType": "AtomicTag" } ] } ] }

Haha I feel you!

Oh hey, I have code in python for that already, but I'm not at my laptop... :confused: I will be in a couple hours though

1 Like

That would be awesome. I'm hoping to have some working solution by mid next week if possible. It's not the end of the world if you can't get to it. I know it's all out of the kindness of our hearts to help each other out and for that I'm grateful.

1 Like

When I'm back I'll post it. I needed it whenever I created udts from a selection of standard tags because when you just do a find replace kn the json to replace the elements with parameters it makes them static not bindings. My script goes through and converts them to bindings

1 Like

I'm sorry, I never did get to trying out your script. I had the bug to learn this jq stuff as I figured it could be useful for a lot of things and be quite elegant in it's purpose. That being said, I thought I'd share what finally worked for me. Ultimately, I learned that you can install Ubuntu on windows and seems to be a cleaner interface to use jq from than windows powershell.

jq 'walk(if type == "object" and .valueSource == "opc" and .name == "Service" then {"name": "Service","typeId": "Logix/ControlBit","parameters":{"ParentPath":{"dataType": "String","value": (.opcItemPath)},"BaseScanClass": {"dataType": "String","value": (.tagGroup)}},"tagType": "UdtInstance","tags": [{"name": "Output","tagType": "AtomicTag"},{"name": "AccessLevel","tagType": "AtomicTag"},{"name": "_rev","tagType": "AtomicTag"},{"name": "LogRequired","tagType": "AtomicTag"}]} else . end)' serviceExport.json > serviceImport.json

Here's a snapshot of my original test tags. I wanted to change the Service Tag (OPC) for Pump 1 to be a UDT like is show under Pump 2.
image

I exported these 2 folders to a file called serviceExport.json. Then in Ubuntu, I simply just read the file to view it's contents. Mostly for comparison before and after.

Then I run the query I pasted above. I've taken a screenshot to point out some of the details of what is going on.

Then I simply import my modified json.
image

Worked beautifully.

1 Like

Oh wow, I think I completely misread your OP and question... I thought you wanted to convert OPC tags that were changed to memory tags for testing, back to OPC tags.
The equivalent of giving you a lamb yiros when you ordered a pizza