Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the index path of found values using jq?

Tags:

Say I have a JSON like this:

{
  "json": [
    "a", 
    [
      "b", 
      "c", 
      [
        "d", 
        "foo", 
        1
      ], 
      [
        [
          42, 
          "foo"
        ]
      ]
    ]
  ]
}

And I want an array of jq index paths that contain foo:

[
    ".json[1][2][1]",
    ".json[1][3][0][1]"
]

Can I achieve this using jq and how? I tried recurse | .foo to get the matches first but I receive an error: Cannot index array with string "foo".