jqを使用してネストされた要素を検索する

jqを使用してネストされた要素を検索する

親要素が動的に変更される("Oauth2"、"Oauth2c")オブジェクトのJSONリスト(以下に1つのオブジェクトが提供されている)があるため、オブジェクトを指定せずにネストされたレベル要素の例: - "scopes"を取得する方法下のパス?

{
    "Oauth2": {
        "description": "Oauth 2.0 implicit authentication",
        "flows": {
            "implicit": {
                "authorizationUrl": "https://accounts.google.com/o/oauth2/auth",
                "scopes": {
                    "https://www.googleapis.com/auth/youtube": "Manage your YouTube account",
                    "https://www.googleapis.com/auth/youtube.channel-memberships.creator": "See a list of your current active channel members, their current level, and when they became a member",
                    "https://www.googleapis.com/auth/youtube.force-ssl": "See, edit, and permanently delete your YouTube videos, ratings, comments and captions",
                    "https://www.googleapis.com/auth/youtube.readonly": "View your YouTube account",
                    "https://www.googleapis.com/auth/youtube.upload": "Manage your YouTube videos",
                    "https://www.googleapis.com/auth/youtubepartner": "View and manage your assets and associated content on YouTube",
                    "https://www.googleapis.com/auth/youtubepartner-channel-audit": "View private information of your YouTube channel relevant during the audit process with a YouTube partner"
                }
            }
        },
        "type": "oauth2"
    },
    "Oauth2c": {
        "description": "Oauth 2.0 authorizationCode authentication",
        "flows": {
            "authorizationCode": {
                "authorizationUrl": "https://accounts.google.com/o/oauth2/auth",
                "scopes": {
                    "https://www.googleapis.com/auth/youtube": "Manage your YouTube account",
                    "https://www.googleapis.com/auth/youtube.channel-memberships.creator": "See a list of your current active channel members, their current level, and when they became a member",
                    "https://www.googleapis.com/auth/youtube.force-ssl": "See, edit, and permanently delete your YouTube videos, ratings, comments and captions",
                    "https://www.googleapis.com/auth/youtube.readonly": "View your YouTube account",
                    "https://www.googleapis.com/auth/youtube.upload": "Manage your YouTube videos",
                    "https://www.googleapis.com/auth/youtubepartner": "View and manage your assets and associated content on YouTube",
                    "https://www.googleapis.com/auth/youtubepartner-channel-audit": "View private information of your YouTube channel relevant during the audit process with a YouTube partner"
                },
                "tokenUrl": "https://accounts.google.com/o/oauth2/token"
            }
        },
        "type": "oauth2"
    }
}

ベストアンサー1

scopesサンプルデータのオブジェクトは式を介してjqアクセスできます。

.[].flows[].scopes

[]パスワイルドカードとして機能します。代わりに、オブジェクトのすべての部分を提供します。

サンプルデータを考えると、次のようになります。

$ jq '.[].flows[].scopes' file.json
{
  "https://www.googleapis.com/auth/youtube": "Manage your YouTube account",
  "https://www.googleapis.com/auth/youtube.channel-memberships.creator": "See a list of your current active channel members, their current level, and when they became a member",
  "https://www.googleapis.com/auth/youtube.force-ssl": "See, edit, and permanently delete your YouTube videos, ratings, comments and captions",
  "https://www.googleapis.com/auth/youtube.readonly": "View your YouTube account",
  "https://www.googleapis.com/auth/youtube.upload": "Manage your YouTube videos",
  "https://www.googleapis.com/auth/youtubepartner": "View and manage your assets and associated content on YouTube",
  "https://www.googleapis.com/auth/youtubepartner-channel-audit": "View private information of your YouTube channel relevant during the audit process with a YouTube partner"
}
{
  "https://www.googleapis.com/auth/youtube": "Manage your YouTube account",
  "https://www.googleapis.com/auth/youtube.channel-memberships.creator": "See a list of your current active channel members, their current level, and when they became a member",
  "https://www.googleapis.com/auth/youtube.force-ssl": "See, edit, and permanently delete your YouTube videos, ratings, comments and captions",
  "https://www.googleapis.com/auth/youtube.readonly": "View your YouTube account",
  "https://www.googleapis.com/auth/youtube.upload": "Manage your YouTube videos",
  "https://www.googleapis.com/auth/youtubepartner": "View and manage your assets and associated content on YouTube",
  "https://www.googleapis.com/auth/youtubepartner-channel-audit": "View private information of your YouTube channel relevant during the audit process with a YouTube partner"
}

また、使用することができます

jq '.. | select(.scopes?).scopes' file.json

これにより、上記のサンプル文書と同じ出力が提供されます。scopesその後、キーを含むすべての項目を検索し、そのキーの内容を抽出します。

jq以前のバージョンを使用する必要があります。

jq '.. | select(type == "object" and has("scopes")).scopes' file.json

おすすめ記事