jq select 路径子集 selected
jq select a subset of paths selected
我得到的多个路径数组包含以下关键字 'headline':
jq -c 'paths | select(.[-1] == "headline")' nytimeseuro.json
["data","legacyCollection","collectionsPage","stream","edges",0,"node","headline"]
["data","legacyCollection","collectionsPage","stream","edges",1,"node","headline"]
["data","legacyCollection","collectionsPage","stream","edges",2,"node","headline"]
["data","legacyCollection","collectionsPage","stream","edges",3,"node","headline"]
["data","legacyCollection","collectionsPage","stream","edges",4,"node","headline"]
["data","legacyCollection","headline"]
我可以 select 它们基于数组索引吗?
就像输出[0:2] select 只是
["data","legacyCollection","collectionsPage","stream","edges",0,"node","headline"]
["data","legacyCollection","collectionsPage","stream","edges",1,"node","headline"]
["data","legacyCollection","collectionsPage","stream","edges",2,"node","headline"]
或输出[-1] select只是
["data","legacyCollection","headline"]
我尝试了下面的命令,但这似乎不是我想要的,它只是将我得到的 每个路径 切片,而不是 select 所有路径的子集 返回:
jq -c 'paths | select(.[-1] == "headline")[:3]' nytimeseuro.json
["data","legacyCollection","collectionsPage"]
["data","legacyCollection","collectionsPage"]
["data","legacyCollection","collectionsPage"]
["data","legacyCollection","collectionsPage"]
["data","legacyCollection","collectionsPage"]
["data","legacyCollection","headline"]
paths
生成一个流,而您显然想要一个数组,您可以从中轻松提取感兴趣的项目。例如:
[paths | select(.[-1] == "headline")][0:2]
Stream-oriented 替代品也可用,例如限制(2;路径....)
我得到的多个路径数组包含以下关键字 'headline':
jq -c 'paths | select(.[-1] == "headline")' nytimeseuro.json
["data","legacyCollection","collectionsPage","stream","edges",0,"node","headline"]
["data","legacyCollection","collectionsPage","stream","edges",1,"node","headline"]
["data","legacyCollection","collectionsPage","stream","edges",2,"node","headline"]
["data","legacyCollection","collectionsPage","stream","edges",3,"node","headline"]
["data","legacyCollection","collectionsPage","stream","edges",4,"node","headline"]
["data","legacyCollection","headline"]
我可以 select 它们基于数组索引吗? 就像输出[0:2] select 只是
["data","legacyCollection","collectionsPage","stream","edges",0,"node","headline"]
["data","legacyCollection","collectionsPage","stream","edges",1,"node","headline"]
["data","legacyCollection","collectionsPage","stream","edges",2,"node","headline"]
或输出[-1] select只是
["data","legacyCollection","headline"]
我尝试了下面的命令,但这似乎不是我想要的,它只是将我得到的 每个路径 切片,而不是 select 所有路径的子集 返回:
jq -c 'paths | select(.[-1] == "headline")[:3]' nytimeseuro.json
["data","legacyCollection","collectionsPage"]
["data","legacyCollection","collectionsPage"]
["data","legacyCollection","collectionsPage"]
["data","legacyCollection","collectionsPage"]
["data","legacyCollection","collectionsPage"]
["data","legacyCollection","headline"]
paths
生成一个流,而您显然想要一个数组,您可以从中轻松提取感兴趣的项目。例如:
[paths | select(.[-1] == "headline")][0:2]
Stream-oriented 替代品也可用,例如限制(2;路径....)