简体   繁体   English

使用 jq 查找嵌套的 JSON 对象

[英]Find nested JSON objects using jq

Is there a way to find the location, and values, of specific objects in heavily-nested JSON files, using jq?有没有办法使用 jq 在高度嵌套的 JSON 文件中查找特定对象的位置和值?

I never know where they are going to occur in the structure of the JSON, and I never know the names of the parents.我永远不知道它们会在 JSON 的结构中出现在哪里,我也不知道父母的名字。 I just know the name of the object I'm searching for.我只知道我正在搜索的 object 的名称。

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".例如,我试图找到“FIND_ME”对象在以下 JSON 中的位置,与整个 JSON 结构相关,但也包括“FIND_ME”中包含的值。 So for this JSON:所以对于这个 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:理想情况下,我会得到以下 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.这样我就可以检查大量文件以确保“FIND_ME”没有出现在不应该出现的地方,从而导致错误。 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它给出了“FIND_ME”的值,像这样

{
  "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.但它不显示父结构。

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演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM