jq – retrieve values from a json table for specific columns in a row

Presumably you would want the output to be grouped by row, so let’s first consider:

.rows[].cells
| map(select(.columnId=="nameColumn" or .columnId=="infoColumn"))

This produces a stream of JSON arrays, the first of which using your main example would be:

[
  {
    "columnId": "nameColumn",
    "value": "name1"
  },
  {
    "columnId": "infoColumn",
    "value": "info1"
  }
]

If you want the output in some alternative format, then you could tweak the above jq program accordingly.

If you wanted to select a large number of columns, the use of a long “or” expression might become unwieldy, so you might also want to consider using a “whitelist”. See e.g. Whitelisting objects using select

Or you might want to use del to delete the unwanted columns.

Producing CSV

One way would be to use @csv with the -r command-line option, e.g. with:

| map(select(.columnId=="nameColumn" or .columnId=="infoColumn")
      | {(.columnId): .value} )
| add
| [.nameColumn, .infoColumn]
| @csv

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top