【问题标题】:Joining multiple objects under a single object and adding sibling objects using jq -f .jq在单个对象下连接多个对象并使用 jq -f .jq 添加同级对象
【发布时间】:2021-10-13 12:33:53
【问题描述】:

所以从我收集到的jq does support 使用-f 多次。但是我不确定这是否是我想要的。

所以我有:

cats.json

{
  "cats": [
    {
      "name": "fluffles",
      "age": 10,
      "color": "white"
    }
  ]
}

dogs.json

{
  "dogs": [
    {
      "name": "sam",
      "age": 5,
      "color": "black and white"
    },
    {
      "name": "rover",
      "age": 2,
      "color": "brown and white"
    }
  ]
}

snakes.json

{
  "snakes": [
    {
      "name": "noodles",
      "age": 10,
      "color": "green"
    }
  ]
}

我能够合并这个对象:

owners.json

{
  "owners": [
    "peter",
    "william",
    "sally"
  ]
}

使用

jq -n -f program.jq owners.json $(ls *.json | grep -v 'owners.json')

其中包含jq 程序

input as $owners | {$owners, animals: [inputs]}

按照in the reply的建议。

但是,如果我想合并两个额外的对象,我不确定该怎么做,比如说我有:

food.json

{
  "food": [
    "meat",
    "fish",
    "vegetables"
  ]
}

以及我想要在顶部,导致:

{
  "owners": [
    "peter",
    "william",
    "sally"
  ],
  "food": [
    "meat",
    "fish",
    "vegetables"
  ],
  "animals": [
    {
      "cats": [
        {
          "name": "fluffles",
          "age": 10,
          "color": "white"
        }
      ]
    },
    {
      "dogs": [
        {
          "name": "sam",
          "age": 5,
          "color": "black and white"
        },
        {
          "name": "rover",
          "age": 2,
          "color": "brown and white"
        }
      ]
    },
    {
      "snakes": [
        {
          "name": "noodles",
          "age": 10,
          "color": "green"
        }
      ]
    }
  ]
}

【问题讨论】:

  • ls | grep 的用途是什么?它是generally an antipattern,我很难看到这里的值(如果你想在命令行上扩展*owners.json* 以列出具有它作为子字符串的文件,你可以在没有ls 的情况下这样做)。
  • jq 和 gojq 都不支持多个 -f FILE 选项,除非第一个这样的规范被忽略。您误读了您引用的(已关闭)“问题”。由于多次使用 -f 选项确实与问题无关,为避免混淆,我建议您只需删除 Q 的第一段。
  • @CharlesDuffy - 你建议ls *.jq | grep -v '^owners\.json$ 的替代品是什么? (注意 -v 选项。)
  • @peak,启用 extglob,!(*owners.json*).jq。但是*.jq 怎么可能一开始就匹配owners.json 呢?
  • 我通过将 slurped 文件移动到与其他 .json 文件不同的位置来简化它,这意味着我不再需要 grep 选项,并且它们不会被读取两次。例如:jq -n -f program.jq --slurpfile owners /slurp/owners.json --slurpfile food /slurp/food.json /animals/*.json 带有 jqprogram:{$owners, food: $food[0].food, animals: [inputs]}

标签: json shell command-line jq


【解决方案1】:

只需使用参数--slurpfile food food.json 读入food.json 并修改program.jq 以包含它:input as $owners | {$owners, food: $food[0].food, animals: [inputs]}

【讨论】:

  • 我无法让它工作。 input as $owners | {$owners, food: $food[0].food, animals: [inputs]},然后用jq -n -f program.jq owners.json -s food food.json 我得到jq: error: $food is not defined at <top-level>, line 1: input as $owners | {$owners, food: $food[0].food, animals: [inputs]}
  • jq -s '[ .[0] * .[1] * .[2] ] as $all | .[3].owners as $owners | .[4].food as $food | { "animals": $all, "owners": $owners, "food": $food }' *.json owners.json food.json 我得到"food": null
  • 你使用了-s,这是--slurp的简写,而不是我打算使用的--slurpfile
  • 成功:确实有效:jq -n -f program.jq owners.json --slurpfile food food.json $(ls *.json | grep -v 'owners.json') 我对 -s 不好。你知道 jq 程序是否可以从多个方法中获取输入吗?
  • @dogman, ...如果您想要使用-n 使默认输入为空并使用--slurpfile 100% 的输入,您可以这样做。
猜你喜欢
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
  • 2019-05-08
  • 1970-01-01
  • 1970-01-01
  • 2018-12-31
  • 2018-09-15
相关资源
最近更新 更多