我有一个 JSON 对象列表(下面给出一个对象),其中父元素将动态更改(“Oauth2”、“Oauth2c”),因此,如何检索嵌套级别元素 Ex:- “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