使用 jq -f .jq 在单个对象下加入多个对象并添加兄弟对象

Joining multiple objects under a single object and adding sibling objects using jq -f .jq

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

所以我有:

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]}

按照建议

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

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"
        }
      ]
    }
  ]
}

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