'Find and append an string in json file using jq

I've a Json file meta.json with the below content and I want to do find and append operation in the below json file using jq filter. For example the below json value of name is demo so, append an string to it like -test so the final value will be demo-test

{
  "version": "2.2.0",
  "vname": "tf",
  "data": {
    "name": "demo",
    "udn": {
      "description": "The `main` tf in this template creates a resource`. "
    }
  }
}

The updated json file should contain the data like below

{
  "version": "2.2.0",
  "vname": "tf",
  "data": {
    "name": "demo-test",
    "udn": {
      "description": "The `main` tf in this template creates a resource`. "
    }
  }
}

Does JQ filter supports this? how we can do this?



Solution 1:[1]

Using jq

$ jq '.data.name |= . + "-test"' meta.json
{
  "version": "2.2.0",
  "vname": "tf",
  "data": {
    "name": "demo-test",
    "udn": {
      "description": "The `main` tf in this template creates a resource`. "
    }
  }
}

Using sed

$ sed '/\<name\>/s/[[:punct:]]\+$/-test&/' meta.json
{
  "version": "2.2.0",
  "vname": "tf",
  "data": {
    "name": "demo-test",
    "udn": {
      "description": "The `main` tf in this template creates a resource`. "
    }
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HatLess