拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 如果档案有一个阵列并且JQ过滤器使用group_by,如何将JQ处理作为多个字典来解决?

如果档案有一个阵列并且JQ过滤器使用group_by,如何将JQ处理作为多个字典来解决?

白鹭 - 2022-03-15 1946 0 0

我有以下 JSON 档案

[
  {
    "id": 6,
    "description": "Component 1",
    "due": "20211122T183000Z",
    "entry": "20211119T181735Z",
    "modified": "20211119T181735Z",
    "project": "product1",
    "status": "pending",
    "uuid": "55bf0497-208c-492a-8f76-bb692d48afaa",
    "tags": [
      "abc",
      "123"
    ],
    "urgency": 13.9699
  },
  {
    "id": 10,
    "description": "Component 2",
    "due": "20211129T183000Z",
    "entry": "20211130T045620Z",
    "modified": "20211130T045620Z",
    "project": "product2",
    "status": "pending",
    "uuid": "d57eb8f7-e5ec-497c-ac47-f1cf34b005db",
    "tags": [
      "foo",
      "bar"
    ],
    "urgency": 14.0151
  },
  {
    "id": 11,
    "description": "Component 3",
    "due": "20211202T183000Z",
    "entry": "20211130T121529Z",
    "completed": "20211130T123915Z",
    "project": "product3",
    "status": "pending",
    "uuid": "9f15e6a4-5cef-4b0f-915b-fc916ab152c7",
    "tags": [
      "xyz",
      "676"
    ],
    "urgency": 14.0096
  },
  {
    "id": 12,
    "description": "Component 4",
    "due": "20211202T183000Z",
    "entry": "20211130T122537Z",
    "pending": "20211130T122537Z",
    "project": "product1",
    "status": "pending",
    "uuid": "91c9ec76-42a7-4ebc-9649-b3a12027feb1",
    "tags": [
      "def"
    ],
    "urgency": 13.9096
  }
]

我在 JQ 过滤器下面写了决议 JSON,预期的输出不是生成多个字典。

group_by(.project,.status)  
         | .[] 
         | { project: .[0].project , status: .[0].status , 
             description: [{"\(.[].description)" : (.[].tags | join(";"))}] }

应用过滤器后,multiple dictionaries由于tags阵列,我得到以下输出

{
  "project": "product1",
  "status": "pending",
  "description": [
    {
      "Component 1": "abc;123"
    },
    {
      "Component 1": "def"
    },
    {
      "Component 4": "abc;123"
    },
    {
      "Component 4": "def"
    }
  ]
}
{
  "project": "product2",
  "status": "completed",
  "description": [
    {
      "Component 2": "foo;bar"
    }
  ]
}
{
  "project": "product3",
  "status": "completed",
  "description": [
    {
      "Component 3": "xyz;676"
    }
  ]
}

我期待的输出没有multiple dictionaries如下

{
  "project": "product1",
  "status": "pending",
  "description": [
    {
      "Component 1": "abc;123"
    },
    {
      "Component 4": "def"
    }
  ]
}
{
  "project": "product2",
  "status": "completed",
  "description": [
    {
      "Component 2": "foo;bar"
    }
  ]
}
{
  "project": "product3",
  "status": "completed",
  "description": [
    {
      "Component 3": "xyz;676"
    }
  ]
}

如何使用 JQ 生成上述预期的输出?

uj5u.com热心网友回复:

与您类似的一种选择是

jq 'group_by(.project)[] 
             | { project: .[0].project, status:.[0].status, "description": [.[] 
             | { (.description) : .tags|join(";") } ] }'

Demo

uj5u.com热心网友回复:

只是汇集.description.tags使用

jq '.[] | del(.description, .tags)   ({(.description): .tags | join(";")})'

演示

也GROUP BY.project和公正的考虑.project.status并与一个阵列.description,并.tags从上面走

jq '
  group_by(.project)[]
  | (first | {project, status})
    {description: map({(.description): .tags | join(";")})}
'

演示

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *