jq-合并具有相同键/值的对象的值,并将它们添加到相关对象的数组中 [英] jq - merge values of objects having the same key/value and add them to an array in a related object

查看:50
本文介绍了jq-合并具有相同键/值的对象的值,并将它们添加到相关对象的数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该问题更为复杂,将此问题带入了更高的层次.

The question is a bit more sophisticated and takes this question to the next level.

我想举一个例子来描述我的问题.假设我输入以下内容:

I would like to present an example to describe my problem. Let's say I have the following input:

[
  {
    "endpoint_url": "assignment/import",
    "body_payload": {
      "name": "assignment1",
      "description": "assignment 1 description"
    }
  },
  {
    "endpoint_url": "assignment-role/create",
    "body_payload": {
      "roleName": "role1",
      "assignmentName": "assignment1"
    }
  },
  {
    "endpoint_url": "assignment-role/create",
    "body_payload": {
      "roleName": "role2",
      "assignmentName": "assignment1"
    }
  },
  {
    "endpoint_url": "assignment/import",
    "body_payload": {
      "name": "assignment2",
      "description": "assignment 2 description"
    }
  },
  {
    "endpoint_url": "assignment-role/create",
    "body_payload": {
      "roleName": "role3",
      "assignmentName": "assignment2"
    }
  },
  {
    "endpoint_url": "assignment-role/create",
    "body_payload": {
      "roleName": "role4",
      "assignmentName": "assignment2"
    }
  },
  {
    "endpoint_url": "assignment-role/create",
    "body_payload": {
      "roleName": "role5",
      "assignmentName": "assignment2"
    }
  },
  {
    "endpoint_url": "assignment/import",
    "body_payload": {
      "name": "assignment3",
      "description": "assignment 3 description"
    }
  },
  {
    "endpoint_url": "assignment-role/create",
    "body_payload": {
      "roleName": "role6",
      "assignmentName": "assignment3"
    }
  },
]

预期的输出可能大致如下:

An exepected output can be along the lines:

[
  {
    "endpoint_url": "assignment/import",
    "body_payload": {
      "name": "assignment1",
      "description": "assignment 1 description",
      "roles": [ "role1", "role2" ]
    }
  },
  {
    "endpoint_url": "assignment/import",
    "body_payload": {
      "name": "assignment2",
      "description": "assignment 2 description",
      "roles": [ "role3", "role4", "role5" ]
    }
  },
  {
    "endpoint_url": "assignment/import",
    "body_payload": {
      "name": "assignment3",
      "description": "assignment 3 description",
      "roles": [ "role6" ]
    }
  }
]

希望您能看到我希望做的以下事情.对于每个具有 assignment/import 的对象,我想提取相应的 assignment-role/create 的所有角色(具有 assignment-role/create ,并且其 assignmentName 等于具有 assignment/import 的相应对象的 name ),并将这些角色添加到其中.我的描述可能有点笨拙,但我希望该示例能使本质有所体现.

I hope you can see I wish to do the following. For each object having an assignment/import I would like to extract all roles of the corresponding assignment-role/create (objects having an assignment-role/create and whose assignmentName equals the name of the corresponding object having an assignment/import) and add these roles to it.
My description may be a bit clumsy but I hope the example gives the essence away.

推荐答案

您在数组中有两种不同的对象类型.它们通过 .body_payload 的子键来区分,因此让我们对所有键进行解构并检查哪些键不为空.我们将为此使用 reduce/2 ,因为我们需要重复更新树的相同部分.

You've got two different object types in your array. They are distinguished by the child keys of .body_payload, so let's destructure all keys and check which ones are not null. We'll use reduce/2 for this since we need to update the same parts of our tree repeatedly.

reduce .[] as
  { body_payload:
    { $name
    , $description
    , $roleName
    , $assignmentName
    }
  }
( null
; if $name and $description
  then .[$name] += {$name, $description}

  elif $roleName and $assignmentName
  then .[$assignmentName].roles += [$roleName]

  else error("some required parameter is null")
  end
)

这将产生一个形式为

{
  "assignment1": {
    "name": "assignment1",
    "description": "assignment 1 description",
    "roles": [
      "role1",
      "role2"
    ]
  },
  "assignment2": {
     ...
  },
  ...
}

我们可以使用 to_entries | map 将其转换为所需的对象数组.

which we can convert to the desired array of objects with to_entries|map.

| to_entries
| map
  ( { endpoint_url: "assignment/import"
    , body_payload: .value
    }
  )

这篇关于jq-合并具有相同键/值的对象的值,并将它们添加到相关对象的数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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