0

Given following input data

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

I would like to get following output

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

I got close with this jsonpath expression $.foos[:]..bars[:].id, but it changes the order of things and results in following output

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

As you can see nested elements are appended to the end of the output, but I would need them to be below the parent element.

Would be great if this was possible with one jsonpath expression, otherwise I will have to write a recursive function.

Edit: changed the id's becauase they can be actually some random number/string

Alex_M
  • 1,824
  • 1
  • 14
  • 26

1 Answers1

1

Just change when to go recursive $.foos[:].bars[:]..id

Gives

[
  "1",
  "2",
  "9",
  "8",
  "3"
]
Balastrong
  • 4,336
  • 2
  • 12
  • 31