kubernetes popeye report JSON to cvs with JQ

12/14/2021

I need to reformat the Popeye Kubernetes report in a spreadsheet. I used jq but it's a bit tricky.

{
  "popeye": {
    "score": 90,
    "grade": "A",
    "sanitizers": [
      {
        "sanitizer": "cluster",
        "tally": {
          "ok": 1,
          "info": 0,
          "warning": 0,
          "error": 0,
          "score": 100
        },
        "issues": {
          "Version": [
            {
              "group": "__root__",
              "level": 0,
              "message": "[POP-406] K8s version OK"
            }
          ]
        }
      }
    ]
  }
}

The best format to export to csv would be something like :

{
  "sanitizer" : "cluster",
  "kube-object" : "Version",
  "group": "__root__",
  "level": 0,
  "message": "[POP-406] K8s version OK"
}

I tried a lot of jq command without success.

Any ideas ?

Thanks.

-- Cyril
csv
export-to-csv
jq
kubernetes

2 Answers

12/14/2021

You are asking for a CSV export but you are showing an object as desired format. So, I interpreted the object's fields as CSV columns:

["sanitizer", "kube-object", "group", "level", "message"],
(.popeye.sanitizers[] | [.sanitizer] + (
  .issues | to_entries[] | [.key, (.value[] | .group, .level, .message)])
)
| @csv
"sanitizer","kube-object","group","level","message"
"cluster","Version","__root__",0,"[POP-406] K8s version OK"

Demo

Use jq's --raw-output or -r parameter to get proper CSV formatting. Also, remove the first line if you don't need headers.

-- pmf
Source: StackOverflow

12/14/2021

One option would be using map() along with + operator in order to produce the JSON as in the format presented within the question such as

jq - r '.[].sanitizers | map({sanitizer}+{"kube-object" : "Version"}+.issues.Version[])[]'

where

{"kube-object" : "Version"}

has been added as a non-existing key-value pair for the source JSON

<kbd>Demo</kbd>

If your aim is to generate comma-seperated key-value pairs line by line, then consider using

jq -r '.[].sanitizers | map({sanitizer}+{"kube-object" : "Version"}+.issues.Version[])[] | to_entries[] | "\(.key), \(.value)"'

<kbd>Demo</kbd>

-- Barbaros &#214;zhan
Source: StackOverflow