jq를 사용하여 중첩된 요소 검색

jq를 사용하여 중첩된 요소 검색

상위 요소가 동적으로 변경되는("Oauth2", "Oauth2c") 개체의 JSON 목록(아래에 하나의 개체가 제공됨)이 있으므로 개체를 지정하지 않고 중첩된 수준 요소 예:- "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

관련 정보