Home > Mobile >  find a specific value in json file and append suffix to it using jq
find a specific value in json file and append suffix to it using jq

Time:05-13

I want to match a specific value in json file and append an suffix to it in the same json file.

Here is my json file

{
  "apiVersion": "argoproj.io/v1alpha1",
  "kind": "Workflow",
  "metadata": {
    "generateName": "kns-vm-"
  },
  "spec": {
    "templates": [{
      "inputs": {
        "parameters": [{
          "name": "un",
          "value": "org"
        }]
      },
      "dag": {
        "tasks": [{
            "name": "create-ns",
            "templateRef": {
              "name": "wft-kn"
            },
            "arguments": {
              "parameters": [{
                "name": "un",
                "value": "{{inputs.parameters.un}}"
              }]
            }
          },
          {
            "name": "create-sm",
            "templateRef": {
              "name": "wft-sn"
            },
            "arguments": {
              "parameters": [{
                "name": "un",
                "value": "{{inputs.parameters.un}}"
              }]
            }
          }
        ]
      }
    }]
  }
}

in this I want to find value wft-sn and append an suffix -test to it

so the updated json file should look like this

{
  "apiVersion": "argoproj.io/v1alpha1",
  "kind": "Workflow",
  "metadata": {
    "generateName": "kns-vm-"
  },
  "spec": {
    "templates": [{
      "inputs": {
        "parameters": [{
          "name": "un",
          "value": "org"
        }]
      },
      "dag": {
        "tasks": [{
            "name": "create-ns",
            "templateRef": {
              "name": "wft-kn"
            },
            "arguments": {
              "parameters": [{
                "name": "un",
                "value": "{{inputs.parameters.un}}"
              }]
            }
          },
          {
            "name": "create-sm",
            "templateRef": {
              "name": "wft-sn-test"
            },
            "arguments": {
              "parameters": [{
                "name": "un",
                "value": "{{inputs.parameters.un}}"
              }]
            }
          }
        ]
      }
    }]
  }
}

I am using the jq command like below but it is not working

jq '. | select(.spec.templates[].dag.tasks[].templateRef.name == "wft-sn").name |= . "-test"' demo.json

what mistake I am doing here? how we can do this?

CodePudding user response:

select reproduces its input if the condition evaluates to true. Apply it therefore only to the part you want to test. Also, keep the overall context in parentheses to retain it on top-level for output:

jq '(.spec.templates[].dag.tasks[].templateRef.name | select(. == "wft-sn"))  = "-test"'
{
  "apiVersion": "argoproj.io/v1alpha1",
  "kind": "Workflow",
  "metadata": {
    "generateName": "kns-vm-"
  },
  "spec": {
    "templates": [
      {
        "inputs": {
          "parameters": [
            {
              "name": "un",
              "value": "org"
            }
          ]
        },
        "dag": {
          "tasks": [
            {
              "name": "create-ns",
              "templateRef": {
                "name": "wft-kn"
              },
              "arguments": {
                "parameters": [
                  {
                    "name": "un",
                    "value": "{{inputs.parameters.un}}"
                  }
                ]
              }
            },
            {
              "name": "create-sm",
              "templateRef": {
                "name": "wft-sn-test"
              },
              "arguments": {
                "parameters": [
                  {
                    "name": "un",
                    "value": "{{inputs.parameters.un}}"
                  }
                ]
              }
            }
          ]
        }
      }
    ]
  }
}

I have also removed the unnecessary . | and reduced |= . to =.

  • Related