简体   繁体   English

jq 更新 key:value 对象与开头的匹配字符串

[英]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.我需要更新以下 json 文件值,其中密钥与 substring 匹配。 sample.json样品.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 .现在我需要 append -dev到那些 key 以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:假设您的输入实际上是格式正确且有效的 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:以下jq程序根据需要生成output:

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

Output: Output:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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