无论嵌套级别如何,都以从上到下的顺序读取嵌套元素的属性

Reading properties of nested elements in from-top-to-down order regardless off nesting level

给定以下输入数据

{
  "foos": [
    {
      "id": "foo",
      "bars": [
        {
        "id": "1",
        "bars": []
        },
        {
          "id": "2",
          "bars": [
            {
              "id": "9",
              "bars": []
            },
            {
              "id": "8",
              "bars": []
            }
          ]
        },
        {
          "id": "3",
          "bars": []
        }
      ]
    }
  ]
}

我想获得以下输出

[
  "1",
  "2",
  "9",
  "8",
  "3"
]

我接近这个 jsonpath 表达式 $.foos[:]..bars[:].id,但它改变了事物的顺序并导致以下输出

[
  "1",
  "2",
  "3",
  "9",
  "8"
]

如您所见,嵌套元素附加到输出的末尾,但我需要它们位于父元素下方。

如果这可以用一个 jsonpath 表达式实现,那就太好了,否则我将不得不编写一个递归函数。

编辑:更改了 id,因为它们实际上可能是随机的 number/string

只需更改何时进行递归$.foos[:].bars[:]..id

给予

[
  "1",
  "2",
  "9",
  "8",
  "3"
]