[英]How do I replace a value with a substring of that value?
I have some json that looks like this (very simplified): 我有一些看起来像这样的json(非常简化):
{"files":[
{"date":"2018-01-08T11:38:36+00:00"},
{"date":"2018-01-08T11:38:27+00:00"},
{"date":"2018-01-07T20:02:12+00:00"},
{"date":"2018-01-07T18:23:26+00:00"}
]}
I want to convert the dates to epoch seconds so I can sort them. 我想将日期转换为纪元秒,以便对其进行排序。 But this format doesn't quite match what strptime will accept: 但是这种格式与strptime所接受的格式不完全相同:
$ jq '.files[] | .date |= strptime("%Y-%m-%dT%H:%M:%S%z")' files.json
jq: error (at files.json:6): date "2018-01-08T11:38:36+00:00" does not match format "%Y-%m-%dT%H:%M:%S%z"
So, if I just strip off the last 5 characters I can use the same format, minus the %z. 因此,如果我只去除最后5个字符,则可以使用相同的格式,减去%z。 This works: 这有效:
$ jq '.files[] | .date[0:19]' files.json
"2018-01-08T11:38:36"
"2018-01-08T11:38:27"
"2018-01-07T20:02:12"
"2018-01-07T18:23:26"
So, what am I doing wrong here: 所以,我在这里做错了什么:
$ jq '.files[] | map( .date |= .date[0:19] )' files.json
jq: error (at files.json:6): Cannot index string with string "date"
Thanks to @peak I got it worked out: 多亏@peak,我才得以解决:
$ jq -c '.files[] | .date |= .[0:19]' files.json
{"date":"2018-01-08T11:38:36"}
{"date":"2018-01-08T11:38:27"}
{"date":"2018-01-07T20:02:12"}
{"date":"2018-01-07T18:23:26"}
jq '.files[] | jq'.files [] | map( .date |= .date[0:19] )' files.json map(.date | = .date [0:19])'files.json
There are at least two problems with the above. 以上至少有两个问题。
You can either write .date = .date[0:19]
, or (better): .date |= .[0:19]
您可以编写.date = .date[0:19]
或(更好) .date |= .[0:19]
It's not entirely clear what you want, but if the goal is simply to edit the JSON, then one option would be: 尚不清楚您想要什么,但是如果目标只是编辑JSON,则一种选择是:
.files |= map( .date |= .[0:19] )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.