Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

duplicate headers after convert to csv

In json file:

{
  "issues": [
    {
      "key": "key_1",
      "component": "my_component",
      "textRange": {
        "startLine": 1,
        "endLine": 11,
        "startOffset": 111,
        "endOffset": 1111
      }
    },
    {
      "key": "key2",
      "component": "my component 2",
      "textRange": {
        "startLine": 2,
        "endLine": 22,
        "startOffset": 222,
        "endOffset": 2222
      }
    },
    {
      "key": "my_key3",
      "component": "my component 3",
      "textRange": {
        "startLine": 548,
        "endLine": 548,
        "startOffset": 14,
        "endOffset": 15
      }
    }
  ]
}

I want to convert to csv and add headers.

I try this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

jq -r '.issues[] | ["key","component", "textRange"], [.key,.component, .textRange[]] | @csv' test.issues.json

Result:

"key","component","textRange"
"key_1","my_component",1,11,111,1111
"key","component","textRange"
"key2","my component 2",2,22,222,2222
"key","component","textRange"
"my_key3","my component 3",548,548,14,15

Why headers are duplicate?

>Solution :

You are creating the headers inside the iteration. Pull them up front

["key","component", "textRange"],
(.issues[] | [.key,.component, .textRange[]])
| @csv
"key","component","textRange"
"key_1","my_component",1,11,111,1111
"key2","my component 2",2,22,222,2222
"my_key3","my component 3",548,548,14,15

Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading