简体   繁体   English

使用 jq 从 JSON 文件中递归获取键名

[英]Get key names from JSON file recursively using jq

I can get top-level JSON keys with jq as我可以使用jq作为顶级 JSON 键

jq 'keys'

Eg, see this question: Get key names from JSON file using jq例如,看到这个问题: Get key names from JSON file using jq

And I can enable recursion in jq :我可以在jq启用递归:

jq '..'

As in: Recursive search values by key如: 按键递归搜索值

But jq '.. | keys'但是jq '.. | keys' jq '.. | keys' returns jq: error at <stdin> string has no keys . jq '.. | keys'返回jq: error at <stdin> string has no keys

当 JSON 中的某些值不存在键时,只需忽略键:

jq '.. | keys?'

Ignore non-objects when inspecting for key names:检查键名时忽略非对象:

jq -r '.. | if type == "object" then to_entries[] | .key else empty end' "${LOCALAPPDATA}/Google/Chrome/User Data/default/Bookmarks" | sort --uniq
bookmark_bar
checksum
children
date_added
date_modified
guid
id
name
other
roots
synced
type
url
version

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

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