Home > OS >  Find nested JSON objects using jq
Find nested JSON objects using jq

Time:12-09

Is there a way to find the location, and values, of specific objects in heavily-nested JSON files, using jq?

I never know where they are going to occur in the structure of the JSON, and I never know the names of the parents. I just know the name of the object I'm searching for.

For example, I'm trying to find where the "FIND_ME" objects are in the following JSON, in relation to the JSON structure as a whole, but also the values contained in "FIND_ME". So for this JSON:

{
    "system": {
        "operating_system_1": {
            "FIND_ME": {
                "special_detail_1": "some_detail",
                "special_detail_2": "some_detail"
            },
            "feature_1": {
                "detail_1": "some_detail"
            },
            "feature_2": {
                "detail_2": "some_detail"
            }
        },
        "operating_system_2": {
            "feature_1": {
                "FIND_ME": {
                    "special_detail_3": "some_detail",
                    "special_detail_4": "some_detail"
                },
                "detail_3": "some_detail"
            },
            "feature_2": {
                "detail_4": "some_detail"
            }
        },
        "operating_system_3": {
            "feature_1": {
                "detail_5": "some_detail"
            },
            "feature_2": {
                "detail_6": "some_detail"
            }
        }
    }
}

I'd ideally get the following output:

{
    "system": {
        "operating_system_1": {
            "FIND_ME": {
                "special_detail_1": "some_detail",
                "special_detail_2": "some_detail"
            }
        },
        "operating_system_2": {
            "feature_1": {
                "FIND_ME": {
                    "special_detail_3": "some_detail",
                    "special_detail_4": "some_detail"
                }
            }
        }
    }
}

This is so I can check numerous files to make sure "FIND_ME" isn't appearing somewhere it shouldn't be, causing errors. I've tried the following from other questions:

cat json_file | jq '..|.FIND_ME? | select(. != null)'

Which gives the values of "FIND_ME", like this

{
  "special_detail_1": "some_detail",
  "special_detail_2": "some_detail"
}
{
  "special_detail_3": "some_detail",
  "special_detail_4": "some_detail"
}

but it doesn't display the parent structure.

CodePudding user response:

Try

jq --arg query "FIND_ME" '
  reduce ((paths | select(.[-1] == $query)) as $p | [$p, getpath($p)])
    as $f ({}; setpath($f[0]; $f[1]))
' json_file
{
  "system": {
    "operating_system_1": {
      "FIND_ME": {
        "special_detail_1": "some_detail",
        "special_detail_2": "some_detail"
      }
    },
    "operating_system_2": {
      "feature_1": {
        "FIND_ME": {
          "special_detail_3": "some_detail",
          "special_detail_4": "some_detail"
        }
      }
    }
  }
}

Demo

  • Related