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

How to get jq to output a long dataset (part 2)?

Suppose I have this ndjson file:

{"id": 99, "labeled_values": [["one", "green"], ["two", "red"], ["three", "blue"]]}
{"id": 100, "labeled_values": [["four", "green"], ["five", "red"]]}

How do I get the output below (tab-separated)? That is: id on every line, and the pairs of (value, label) flattened.

99 one green
99 two red
99 three blue
100 four green
100 five red

Here are two failed attempts:

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

$ cat tmp/foo.ndjson | jq -r '.id as $id | [$id, .labeled_values.[0], .labeled_values.[1]] '
[
  99,
  [
    "one",
    "green"
  ],
  [
    "two",
    "red"
  ]
]
[
  100,
  [
    "four",
    "green"
  ],
  [
    "five",
    "red"
  ]
]

$ cat tmp/foo.ndjson | jq -r '.id as $id | [$id, .labeled_values[].[0], .labeled_values[].[1]] '
[
  99,
  "one",
  "two",
  "three",
  "green",
  "red",
  "blue"
]
[
  100,
  "four",
  "five",
  "green",
  "red"
]

This question is very related, but I still don’t understand jq well enough.

A related question: how do I learn enough about the processing model of jq to understand how to break down nested structures like this into flat structures?

Most of the examples I find are flat and simple. They pick out single fields, not nested things.

>Solution :

.[] will create an output for each .labeled_values.

To convert .id to a string and add a \t use a string interpolation.

Use @tsv to convert the array to tsv

"\(.id)\t\(.labeled_values[] | @tsv)"

TIO

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