jq

Object item and nested array items on same output line


I've got some data that looks like this:

{
"data": [
    {
      "startTime": "2024-12-31T00:00:00Z",
      "data": [
        {
          "one": "a",
          "two": 1125.000
        },
        {
          "one": "b",
          "two": 2119.000
        },
        {
          "one": "c",
          "two": 0.000
        }
      ]
    },

with multiple objects in the outer data array. What I'm failing at is outputting something like this:

startTime, one, two

which in the case of the data above would give me three lines of output. I can get one and two together, but not startTime: with jq -r '.data[] | "\(.startTime)", (.data[] | "\(.one), \(.two)" )'

2024-12-31T00:00:00Z
a, 1125.000
b, 2119.000
c, 0.000

jq -r '.data[] | "\(.startTime)", (.data[] | "\(.one), \(.two)" )' gets me the following, iterating over each of the last items ("two"):

2024-12-31T00:00:00Z, a, 1125.000
2024-12-31T00:00:00Z, b, 1125.000
2024-12-31T00:00:00Z, c, 1125.000
2024-12-31T00:00:00Z, a, 2119.000
2024-12-31T00:00:00Z, b, 2119.000
2024-12-31T00:00:00Z, c, 2119.000
2024-12-31T00:00:00Z, a, 0.000
2024-12-31T00:00:00Z, b, 0.000
2024-12-31T00:00:00Z, c, 0.000

I can't seem to hit on the right syntax.


Solution

  • You're looking for something like this:

    .data[] | [.startTime] + (.data[] | [.one, .two]) | join(", ")