Home > Back-end >  Can't parse array in another array - jq: error (at test.issues.json:100): object ({"compon
Can't parse array in another array - jq: error (at test.issues.json:100): object ({"compon

Time:01-29

File test.issues.json

{
  "issues": [
    {
      "key": "key_1",
      "component": "my_component",
      "textRange": {
        "startLine": 1,
        "endLine": 11,
        "startOffset": 111,
        "endOffset": 1111
      },
      "flows": [],
      "status": "OPEN",
      "type": "BUG",
      "scope": "MAIN"
    },
    {
      "key": "key2",
      "component": "my component 2",
      "textRange": {
        "startLine": 2,
        "endLine": 22,
        "startOffset": 222,
        "endOffset": 2222
      },
      "flows": [
        {
          "locations": [
            {
              "component": "some component",
              "textRange": {
                "startLine": 35,
                "endLine": 35,
                "startOffset": 3,
                "endOffset": 50
              },
              "msg": "any  message"
            }
          ]
        },
        {
          "locations": [
            {
              "component": "another component",
              "textRange": {
                "startLine": 36,
                "endLine": 36,
                "startOffset": 3,
                "endOffset": 71
              },
              "msg": "message custom"
            }
          ]
        },
        {
          "locations": [
            {
              "component": "Alarm.java",
              "textRange": {
                "startLine": 37,
                "endLine": 37,
                "startOffset": 3,
                "endOffset": 76
              },
              "msg": "message number 2"
            }
          ]
        },
        {
          "locations": [
            {
              "component": "Alarm.java",
              "textRange": {
                "startLine": 38,
                "endLine": 38,
                "startOffset": 3,
                "endOffset": 50
              },
              "msg": "message number 3"
            }
          ]
        }
      ]
    },
    {
      "key": "my_key3",
      "component": "my component 3",
      "textRange": {
        "startLine": 548,
        "endLine": 548,
        "startOffset": 14,
        "endOffset": 15
      },
      "flows": [],
      "status": "OPEN",
      "type": "CODE_SMELL",
      "scope": "LOCAL"
    }
  ]
}

I need to convert to csv.

I try this:

jq -r '.issues[] | [.key,.component,.textRange[], (.flows[].locations[]), .status, .type, .scope] | @csv' test.issues.json

But I get error:

"key_1","my_component",1,11,111,1111,"OPEN","BUG","MAIN"
jq: error (at test.issues.json:100): object ({"component...) is not valid in a csv row

I need to get smt like this:

key,component,textRange,startLine,endLine,startOffset,endOffset,status,type,scope,flows,locations,component,textRange,startLine,endLine,startOffset,endOffset,msg
key_1,my_component,,1,11,111,1111,OPEN,BUG,MAIN,,,,,,,,,
key2,my component 2,,2,22,222,2222,,,,,,some component,,35,35,3,50,any  message
,,,,,,,,,,,,another component,,36,36,3,71,message custom
,,,,,,,,,,,,Alarm.java,,37,37,3,76,message number 2
,,,,,,,,,,,Alarm.java,,38,38,3,50,message number 3
my_key3,my component 3,,548,548,14,15,OPEN,CODE_SMELL,LOCAL,,,,,,,,,

CodePudding user response:

Try this for starters

jq -r '
  .issues[]
  | (.flows |= first), (.flows[1:][] | {flows:.})
  | [
      .key, .component, (
        .textRange | null, .startLine, .endLine, .startOffset, .endOffset
      ), .status, .type, .scope, (
        .flows.locations[]? // {} | null, null, .component, (
          .textRange | null, .startLine, .endLine, .startOffset, .endOffset
        ), .msg
      )
    ]
  | join(",")
'
key_1,my_component,,1,11,111,1111,OPEN,BUG,MAIN,,,,,,,,,
key2,my component 2,,2,22,222,2222,,,,,,some component,,35,35,3,50,any  message
,,,,,,,,,,,,another component,,36,36,3,71,message custom
,,,,,,,,,,,,Alarm.java,,37,37,3,76,message number 2
,,,,,,,,,,,,Alarm.java,,38,38,3,50,message number 3
my_key3,my component 3,,548,548,14,15,OPEN,CODE_SMELL,LOCAL,,,,,,,,,

Demo

Note: You may want to use @csv instead of join(",") but it'll wrap the strings in quotes.

Also, to add a header line, prepend the filters before the join/@csv with a simple array of strings.

  •  Tags:  
  • Related