使用JQ使用其父键提取某些数组元素

发布于 2025-01-18 03:37:30 字数 624 浏览 0 评论 0原文

我想知道如何使用 jq 从 .json 文件中提取模式

echo '{"parts": [{"name":"core","items":"garbage with ITEM1 ITEM2 and more"},{"name":"misc","items":"ITEM3 ITEM4 ITEM5 bla bla"} ]}' | jq '.parts | .[] | .items |=split(" ")'
{
  "name": "core",
  "items": [
    "garbage",
    "with",
    "ITEM1",
    "ITEM2",
    "and",
    "more"
  ]
}
{
  "name": "misc",
  "items": [
    "ITEM3",
    "ITEM4",
    "ITEM5",
    "bla",
    "bla"
  ]
}

,我认为在拆分项目时,但我不知道如何提取每个 ITEMx。

我想获得这个输出:

{ "core","ITEM1" }
{ "core","ITEM2" }
{ "misc","ITEM3" } 
{ "misc","ITEM4" }
{ "misc","ITEM5" }  

I would like to know how to use jq to extract patterns from a .json file

echo '{"parts": [{"name":"core","items":"garbage with ITEM1 ITEM2 and more"},{"name":"misc","items":"ITEM3 ITEM4 ITEM5 bla bla"} ]}' | jq '.parts | .[] | .items |=split(" ")'
{
  "name": "core",
  "items": [
    "garbage",
    "with",
    "ITEM1",
    "ITEM2",
    "and",
    "more"
  ]
}
{
  "name": "misc",
  "items": [
    "ITEM3",
    "ITEM4",
    "ITEM5",
    "bla",
    "bla"
  ]
}

I think in splitting the items, but I don't know how to extract each ITEMx.

I want to obtain this output:

{ "core","ITEM1" }
{ "core","ITEM2" }
{ "misc","ITEM3" } 
{ "misc","ITEM4" }
{ "misc","ITEM5" }  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

蝶舞 2025-01-25 03:37:31

您所需的输出不是有效的JSON。

您是否要在.Name字段的值下形成一个数组?

jq '.parts[] | {(.name): (.items | split(" "))}'
{
  "core": [
    "garbage",
    "with",
    "ITEM1",
    "ITEM2",
    "and",
    "more"
  ]
}
{
  "misc": [
    "ITEM3",
    "ITEM4",
    "ITEM5",
    "bla",
    "bla"
  ]
}

demo

或您是否希望每个单词形成一个单独的对象?

jq '.parts[] | (.items | split(" "))[] as $word | {(.name): $word}'
{"core":"garbage"}
{"core":"with"}
{"core":"ITEM1"}
{"core":"ITEM2"}
{"core":"and"}
{"core":"more"}
{"misc":"ITEM3"}
{"misc":"ITEM4"}
{"misc":"ITEM5"}
{"misc":"bla"}
{"misc":"bla"}

demo

仅捕获与Regex iteg> Item \ d+d+的单词采用扫描函数而不是拆分:

jq '.parts[] | {(.name): .items | scan("ITEM\\d+")}'
{"core":"ITEM1"}
{"core":"ITEM2"}
{"misc":"ITEM3"}
{"misc":"ITEM4"}
{"misc":"ITEM5"}

demo

Your desired output is not valid JSON.

Do you want the words form an array under the value of the .name field?

jq '.parts[] | {(.name): (.items | split(" "))}'
{
  "core": [
    "garbage",
    "with",
    "ITEM1",
    "ITEM2",
    "and",
    "more"
  ]
}
{
  "misc": [
    "ITEM3",
    "ITEM4",
    "ITEM5",
    "bla",
    "bla"
  ]
}

Demo

Or do you want each word to form a separate object?

jq '.parts[] | (.items | split(" "))[] as $word | {(.name): $word}'
{"core":"garbage"}
{"core":"with"}
{"core":"ITEM1"}
{"core":"ITEM2"}
{"core":"and"}
{"core":"more"}
{"misc":"ITEM3"}
{"misc":"ITEM4"}
{"misc":"ITEM5"}
{"misc":"bla"}
{"misc":"bla"}

Demo

To only capture words that match the regex ITEM\d+, you could employ the scan function instead of splitting:

jq '.parts[] | {(.name): .items | scan("ITEM\\d+")}'
{"core":"ITEM1"}
{"core":"ITEM2"}
{"misc":"ITEM3"}
{"misc":"ITEM4"}
{"misc":"ITEM5"}

Demo

亢潮 2025-01-25 03:37:31

基于您的尝试,我们可以尝试:

.parts
| .[]
| .items |= (split(" ") | map(select(test("ITEM"))))
| {(.name): .items[]}

这会生成一个对象流,例如 {"core":"ITEM1"}。如果您确实想要 Q 中显示的非 JSON 输出,那么添加额外的步骤就很容易了。

Building on your attempt, we could try:

.parts
| .[]
| .items |= (split(" ") | map(select(test("ITEM"))))
| {(.name): .items[]}

This produces a stream of objects such as {"core":"ITEM1"}. If you really want the non-JSON output shown in the Q, it's easy enough to add the additional step.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文