繁体   English   中英

使用jq将一个json文件加载到另一个json文件的哈希中

[英]Using jq to load one json file into the hash of another json file

我试图弄清楚如何使用jq将一个json文件的内容加载到另一个文件的哈希中。 例如:

这个文件:

{
  "annotations": {...},
  "rows": [ {...}, {...}]
}

应该在哈希仪表板中插入此文件:

{
  "dashboard": { },
  "overwrite": true,
  "message": "new commit"
}

所以结果文件应该是

{
  "dashboard": {
     "annotations": {...},
     "rows": [ {...}, {...}]
  },
  "overwrite": true,
  "message": "new commit"
}

我当时想用烟斗来做| 或| =运算符,但我不知道如何使用一个内容并将其分配给另一个文件的选择过滤器。

jq解决方案:

jq --slurpfile annot annot.json '.dashboard |= $annot[0]' dashb.json

--slurpfile variable-name filename

此选项读取命名文件中的所有JSON文本,并将已解析的JSON值数组绑定到给定的全局变量。

如果您的jq没有--slurpfile,则可以例如按以下方式运行jq:

jq -s '.[0] as $fragment | .[1] | (.dashboard |= $fragment)' fragment.json dashboard.json

暂无
暂无

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

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