尝试使用JQ转换JSON中过于复杂的对象数组

o7jaxewo  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(126)

我有一个JSON文档,如下所示:

{
  "name": "My Team",
  "team size": [
    {
      "min": [
        {
          "Leader": 1
        },
        {
          "Followers": 5
        },
        {
          "point cost": 7
        }
      ]
    },
    {
      "max": [
        {
          "Leader": 1
        },
        {
          "Followers": 9
        },
        {
          "point cost": 11
        }
      ]
    }
  ]
}

我希望它看起来像这样:

{
    "name": "My Team",
    "team size": [
        {
            "size": "min",
            "Leader": 1,
            "Followers": 5,
            "point cost": 7
        },
        {
            "size": "max",
            "Leader": 1,
            "Followers": 9,
            "point cost": 11
        }
    ]
}

但是我很难理解它。我知道它有点像jq . '"team size" |= map(to_entries[] | .key + .values),但是我没有弄清楚语法。任何帮助都将非常感谢。

drnojrws

drnojrws1#

以下是其中一种方法:

.["team size"] |=
  (add
   | [ ({size: "min"} + (.min | add)),
       ({size: "max"} + (.max | add)) ] )

或更干燥:

.["team size"] |=
  (add
   | . as $in
   | reduce keys_unsorted[] as $k ([];
       . + [{size: $k} + ($in[$k]|add) ] ))

相关问题