【发布时间】:2016-05-11 15:53:12
【问题描述】:
正如题主所说,我的目标是编写一个all_keys 函数,从任意嵌套的 json blob 中提取所有键,根据需要遍历包含的数组和对象,并输出包含键的数组,不重复。
例如,给定以下输入:
[
{"name": "/", "children": [
{"name": "/bin", "children": [
{"name": "/bin/ls", "children": []},
{"name": "/bin/sh", "children": []}]},
{"name": "/home", "children": [
{"name": "/home/stephen", "children": [
{"name": "/home/stephen/jq", "children": []}]}]}]},
{"name": "/", "children": [
{"name": "/bin", "children": [
{"name": "/bin/ls", "children": []},
{"name": "/bin/sh", "children": []}]},
{"name": "/home", "children": [
{"name": "/home/stephen", "children": [
{"name": "/home/stephen/jq", "children": []}]}]}]}
]
all_keys 函数应该产生这个输出:
[
"children",
"name"
]
为此,我设计了以下函数,但它既慢又复杂,所以我想知道您是否可以想出一个更简洁、更快的方法来获得相同的结果。
def all_keys:
. as $in |
if type == "object" then
reduce keys[] as $k (
[];
. + [$k, ($in[$k] | all_keys)[]]
) | unique
elif type == "array" then (
reduce .[] as $i (
[];
. + ($i | all_keys)
) | unique
)
else
empty
end
;
作为参考,在我的 Intel T9300@2.50GHz CPU 上在 this 53MB json file 上运行该功能大约需要 22 秒(我知道,它很古老,但仍然可以正常工作)。
【问题讨论】:
标签: json parsing scripting command-line-interface jq