Home > Net >  How to solve JQ processing as multiple dictionaries if document has an array and JQ filter uses grou
How to solve JQ processing as multiple dictionaries if document has an array and JQ filter uses grou

Time:12-26

I have the following JSON document

[
  {
    "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
  }
]

I have written below JQ filter to parse the JSON, the expected output is not to generate multiple dictionaries.

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

After applying the filter, i get the below output with multiple dictionaries because of the tags array

{
  "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"
    }
  ]
}

The output I am expecting is without multiple dictionaries as below

{
  "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"
    }
  ]
}

How can I generate the above-expected output using JQ?

CodePudding user response:

To just bring together .description and .tags use

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

Demo

To also group by .project and just consider .project, .status and an array with the .description and .tags from above, go

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

Demo

CodePudding user response:

One similar option to yours would be

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

Demo

  • Related