如何使用jq和map从键值对复杂JSON中获取对象列表? (活跃活动)
我有以下 JSON。我想根据它们的 CC 角色获取键值对对象。在此示例中,有 3 个角色(演示者、审批者、客户)。 Presenter 的类型为 TO
。其他 2 个属于 CC
类型。我想要获取 CC
类型。可以有更多,因为它是动态的。
JSON
{
"Presenter_TO_Email": "[email protected]",
"Approver_CC_Email": "[email protected]",
"Customer_CC_Email": "[email protected]",
"Invoice": "001",
"Date": "2022-02-14"
}
输出
{
"Approver": {
"email_address": "[email protected]",
"role": "Approver"
},
"Customer": {
"email_address": "[email protected]",
"role": "Customer"
}
}
我可以使用 this 示例使用 INDEX 进行操作,但因为我使用的是旧版本的 jq
,它会抛出错误 jq: error: INDEX/2 is not Define at
I have following JSON. I want to get key-value pair objects based on their CC role. In this example there are 3 roles(Presenter, Approver, Customer). Presenter is of type TO
. Other 2 are of type CC
. I want to get of type CC
. There can be more as it is dynamic.
JSON
{
"Presenter_TO_Email": "[email protected]",
"Approver_CC_Email": "[email protected]",
"Customer_CC_Email": "[email protected]",
"Invoice": "001",
"Date": "2022-02-14"
}
Output
{
"Approver": {
"email_address": "[email protected]",
"role": "Approver"
},
"Customer": {
"email_address": "[email protected]",
"role": "Customer"
}
}
I can do using INDEX using this example but as I am using older version of jq
, it throws error jq: error: INDEX/2 is not defined at <top-level>, line 1:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
with_entries
根据键和值进行更改:演示
Use
with_entries
to make changes based on keys and values:Demo
在包含 INDEX/2 的 jq 版本中,它被定义为一个简单的 jq 函数,因此如果您的 jq 不包含它,您可以简单地自己包含它的定义:
In versions of jq which include
INDEX/2
, it is defined as a simple jq function, so if your jq does not include it, you can simply include its definition yourself: