简体   繁体   中英

jq to update key:value objects with matching strings at the begining

I need to update below json file values where key matches a substring. sample.json

{
"A": "a"
"re": {
  "jok/pok": ^20.0,
  "sok/dok": ^23.0,
  "jok/ssd": ^32.0
}
}

now I need to append -dev to those key/values where key starts with jok .

{
"A": "a"
"re": {
  "jok/pok": ^20.0-dev,
  "sok/dok": ^23.0,
  "jok/ssd": ^32.0-dev
}
}

I know I'm missing some really simple solution.

Assuming your input is actually well-formed and valid JSON:

{
  "A": "a",
  "re": {
    "jok/pok": "^20.0",
    "sok/dok": "^23.0",
    "jok/ssd": "^32.0"
  }
}

The following jq program generates the output as required:

.re |= with_entries(select(.key|startswith("jok")).value |= . + "-dev")

Output:

{
  "A": "a",
  "re": {
    "jok/pok": "^20.0-dev",
    "sok/dok": "^23.0",
    "jok/ssd": "^32.0-dev"
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM