用 jq 重构 JSON

Reconstructing JSON with jq

我有一个 JSON 这样的 (sample.json):

{
  "sheet1": [
    {
      "hostname": "sv001",
      "role": "web",
      "ip1": "172.17.0.3"
    },
    {
      "hostname": "sv002",
      "role": "web",
      "ip1": "172.17.0.4"
    },
    {
      "hostname": "sv003",
      "role": "db",
      "ip1": "172.17.0.5",
      "ip2": "172.18.0.5"
    }
  ],
  "sheet2": [
    {
      "hostname": "sv004",
      "role": "web",
      "ip1": "172.17.0.6"
    },
    {
      "hostname": "sv005",
      "role": "db",
      "ip1": "172.17.0.7"
    },
    {
      "hostname": "vsv006",
      "role": "db",
      "ip1": "172.17.0.8"
    }
  ],
  "sheet3": []
}

我想像这样提取数据:

工作表 1

jq'(某命令)'sample.json

{
    "web": {
        "hosts": [
            "172.17.0.3",
            "172.17.0.4"
        ]
    },
    "db": {
        "hosts": [
            "172.17.0.5"
        ]
    }
}

是否可以用jq map进行重建? (我会将结果重新用于 ansible 库存。)

如果目标是根据 ip 在每个 sheet 中的角色重新分组,您可以这样做:

map_values(
    reduce group_by(.role)[] as $g ({};
        .[$g[0].role].hosts = [$g[] | del(.hostname, .role)[]]
    )
)

产生这样的东西:

{
  "sheet1": {
    "db": {
      "hosts": [
        "172.17.0.5",
        "172.18.0.5"
      ]
    },
    "web": {
      "hosts": [
        "172.17.0.3",
        "172.17.0.4"
      ]
    }
  },
  "sheet2": {
    "db": {
      "hosts": [
        "172.17.0.7",
        "172.17.0.8"
      ]
    },
    "web": {
      "hosts": [
        "172.17.0.6"
      ]
    }
  },
  "sheet3": {}
}

https://jqplay.org/s/3VpRc5l4_m

如果您想将所有内容展平到一个只保留唯一 ip 的对象,您可以保持大部分内容相同,您只需要在分组之前展平输入并删除 map_values/1 调用。

$ jq -n '
reduce ([inputs[][]] | group_by(.role)[]) as $g ({};
    .[$g[0].role].hosts = ([$g[] | del(.hostname, .role)[]] | unique)
)
'
{
  "db": {
    "hosts": [
      "172.17.0.5",
      "172.17.0.7",
      "172.17.0.8",
      "172.18.0.5"
    ]
  },
  "web": {
    "hosts": [
      "172.17.0.3",
      "172.17.0.4",
      "172.17.0.6"
    ]
  }
}

https://jqplay.org/s/ZGj1wC8hU3

这是一个简短、直接且有效的解决方案 -- 之所以有效,部分原因是它通过以下通用辅助函数避免了 group_by

def add_by(f;g): reduce .[] as $x ({}; .[$x|f] += [$x|g]);
.sheet1
| add_by(.role; .ip1) 
| map_values( {hosts: .} )

输出

这会产生所需的输出:

{
 "web": {
    "hosts": [
      "172.17.0.3",
      "172.17.0.4"
    ]
  },
  "db": {
    "hosts": [
      "172.17.0.5"
    ]
  }
}