using regular expressions to find and remove json code

95 Views Asked by At

I need to delete over 100,000 tags in a project with ignition. I decide the best way would be to download the json of the files and delete all instances of consistent tags in different devices. Example: I need to delete this tag (and others) in hundreds of devices, they will be identical to all the others accept for the FV5003 where it could be PP2340 or AG7698.

            {
              "valueSource": "opc",
              "opcItemPath": "ns\u003d1;s\u003d[PPD]FV5003_HOA.State.IO_Open",
              "dataType": "Boolean",
              "name": "IO_Open",
              "tagType": "AtomicTag",
              "opcServer": "Ignition OPC UA Server"
            },

Is there a way to delete all instances like this making FV5003 a wild card in regular expressions. I have zero experience in regex. Also I am using vscode.

Any help would be great, Thanks.

1

There are 1 best solutions below

0
On
jq '
  del(
    .. |
    select(type=="object") |
    select(has("opcItemPath")) |
    select(
      .opcItemPath|
      test("\\Ans\u003d1;s\u003d\\[PPD\\].*_HOA.State.IO_Open\\Z")
    )
  )
' input.json
  • .. - walk the structure
  • select:
    • objects
    • that contain an attribute named opcItemPath
    • whose value matches the test regex
  • delete them

Or, to "delete everything within the brackets":

jq '
  (
    .. |
    select(
      .opcItemPath |
      test("\\Ans\u003d1;s\u003d\\[PPD\\].*_HOA.State.IO_Open\\Z")
    )?
  ) |= {}
' input.json
  • |= - update selected objects to be empty object {}
  • ? - discard errors that might arise from eliding the first two select filters