如何使用jq和map从键值对复杂JSON中获取对象列表? (活跃活动)

发布于 2025-01-09 06:03:41 字数 1421 浏览 1 评论 0原文

我有以下 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, line 1:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖不如嘢 2025-01-16 06:03:41

使用 with_entries 根据键和值进行更改:

jq '
  with_entries(
    (.key / "_CC_") as $key | select($key[1])
    | {key: $key[0], value: {email_address: .value, role: $key[0]}}
  )
'
{
  "Approver": {
    "email_address": "[email protected]",
    "role": "Approver"
  },
  "Customer": {
    "email_address": "[email protected]",
    "role": "Customer"
  }
}

演示

Use with_entries to make changes based on keys and values:

jq '
  with_entries(
    (.key / "_CC_") as $key | select($key[1])
    | {key: $key[0], value: {email_address: .value, role: $key[0]}}
  )
'
{
  "Approver": {
    "email_address": "[email protected]",
    "role": "Approver"
  },
  "Customer": {
    "email_address": "[email protected]",
    "role": "Customer"
  }
}

Demo

冧九 2025-01-16 06:03:41

在包含 INDEX/2 的 jq 版本中,它被定义为一个简单的 jq 函数,因此如果您的 jq 不包含它,您可以简单地自己包含它的定义:

def INDEX(stream; idx_expr):
  reduce stream as $row ({};
    .[$row|idx_expr|
      if type != "string" then tojson
      else .
      end] |= $row);

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:

def INDEX(stream; idx_expr):
  reduce stream as $row ({};
    .[$row|idx_expr|
      if type != "string" then tojson
      else .
      end] |= $row);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文