使用jq-f.jq连接单个对象下的多个对象并添加同级对象 [英] Joining multiple objects under a single object and adding sibling objects using jq -f .jq

查看:26
本文介绍了使用jq-f.jq连接单个对象下的多个对象并添加同级对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我多次使用-f收集的jqdoes 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]}

如建议的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"
        }
      ]
    }
  ]
}

推荐答案

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

这篇关于使用jq-f.jq连接单个对象下的多个对象并添加同级对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆