为什么 first(select(.updated_at=="2019-06-03T16:36:53.194Z"))
在输入时返回超过 1 个结果
{"_id":"a","updated_at":"2019-06-03T16:36:53.194Z"}
{"_id":"b","updated_at":"2019-06-03T19:27:15.192Z"}
{"_id":"c","updated_at":"2019-06-03T20:02:11.538Z"}
{"_id":"d","updated_at":"2019-06-03T21:12:05.764Z"}
{"_id":"e","updated_at":"2019-06-04T07:55:04.251Z"}
{"_id":"f","updated_at":"2019-06-03T22:21:37.276Z"}
{"_id":"g","updated_at":"2019-06-03T20:57:42.296Z"}
{"_id":"h","updated_at":"2019-06-03T18:56:07.687Z"}
{"_id":"i","updated_at":"2019-06-04T09:28:08.276Z"}
{"_id":"j","updated_at":"2019-06-04T08:54:05.048Z"}
{"_id":"k","updated_at":"2019-06-03T16:36:53.194Z"}
{"_id":"l","updated_at":"2019-06-03T16:36:53.194Z"}
https://jqplay.org/s/2sm1pKQ2fS
我也尝试过使用 nth
但它似乎也没有预期的行为:
最佳答案
因为,first
的输入是一个对象流,因此它为每个对象运行一次。要为整个流运行一次,请在命令行上指定 -n/--null-input
标志,并使用 inputs
函数访问输入。这样,first
的输入将为 null
,其参数将为从 inputs
select
的对象流, 所以它只能产生第一个。
$ jq -n 'first(inputs | select(.updated_at == "2019-06-03T16:36:53.194Z"))' file
{
"_id": "a",
"updated_at": "2019-06-03T16:36:53.194Z"
}
关于json - 使用 jq 从 json 流中进行第一次匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56442850/