0

I have json file that I am able to loop and get the desired values with the below command. I am trying to gain some insights in displaying these values into a table format. I am using @tsv but getting an error object ({"release":...) cannot be tsv-formatted, only array. How can I get the below output display?

My desired output is shown below:

Release         Installed    Latest    Old      Deprecated
-------         ---------    ------    ---      ----------
test-app        1.0.0        2.0.0     true     false    

jq:

cat test.json | jq '.test[] | select((.outdated or .deprecated) and ((.release|startswith("update")) | not) and ((.release|startswith("upgrade")) | not))'

json:

{
    "test": [{
        "release": "myapp1",
        "Installed": {
            "version": "0.3.0",
            "appVersion": "v1.2.6"
        },
        "Latest": {
            "version": "",
            "appVersion": ""
        },
        "outdated": false,
        "deprecated": false
    }, {
        "release": "myapp2",
        "Installed": {
            "version": "6.5.13",
            "appVersion": "1.9.1"
        },
        "Latest": {
            "version": "",
            "appVersion": ""
        },
        "outdated": false,
        "deprecated": false
    }, {
        "release": "test-app",
        "Installed": {
            "version": "1.0.0",
            "appVersion": ""
        },
        "Latest": {
            "version": "2.0.0",
            "appVersion": ""
        },
        "outdated": true,
        "deprecated": false
    }, {
        "release": "update-app",
        "Installed": {
            "version": "1.0.0",
            "appVersion": ""
        },
        "Latest": {
            "version": "3.0.0",
            "appVersion": ""
        },
        "outdated": true,
        "deprecated": false
    }, {
        "release": "upgrade-app",
        "Installed": {
            "version": "2.0.0",
            "appVersion": ""
        },
        "Latest": {
            "version": "2.0.0",
            "appVersion": ""
        },
        "outdated": false,
        "deprecated": true
    }]
}
peak
  • 105,803
  • 17
  • 152
  • 177
MaryCoding
  • 624
  • 1
  • 9
  • 31

1 Answers1

2

Both @csv and @tsv expect an array as input, so you could just append the following to your jq filter:

| [.release, .Installed.version, .Latest.version, .outdated, .deprecated]
| @tsv
peak
  • 105,803
  • 17
  • 152
  • 177
  • Nice! I did `cat test.json | jq '.test[] | select((.outdated or .deprecated) and ((.release|startswith("update")) | not) and ((.release|startswith("upgrade")) | not)) | [.release, .Installed.version, .Latest.version, .outdated, .deprecated] | @tsv'` and get this result `test-app\t1.0.0\t2.0.0\ttrue\tfalse`. Whats the proper way to add headers? – MaryCoding Mar 12 '21 at 03:36
  • I followed up with another questions about empty values: https://stackoverflow.com/questions/66603976/jq-handling-empty-strings-and-replacing-them-with-a-default-value – MaryCoding Mar 12 '21 at 17:07