无法解析另一个数组中的数组 - jq:错误(在 test.issues.json:100):对象({“组件...”在 csv 行中无效

Can't parse array in another array - jq: error (at test.issues.json:100): object ({"component...) is not valid in a csv row

文件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"
    }
  ]
}

我需要转换成 csv。

我试试这个:

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

但我得到错误:

"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

我需要像这样获取 smt:

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,,,,,,,,,

初学者试试这个

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

注意:您可能想使用 @csv 而不是 join(","),但它会将字符串用引号引起来。

此外,要添加 header 行,请在 join/@csv 之前使用简单的字符串数组添加过滤器。