Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 526

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

$
0
0

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 green99 two red99 three blue100 four green100 five red

Here are two failed attempts:

$ 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.


Viewing all articles
Browse latest Browse all 526

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>