JSON 和 jq 更新多个值
JSON and jq updating multiple values
您好,我正在尝试在单个 "jq" 命令中执行计算,但我不知道如何引用新值。
原版JSON
{
"Status": "Down",
"StatusID": "75328241",
"Start": "2017-03-17 15:06:40",
"End": "2017-03-17 15:08:37",
"Period": "1 Minutes "
}
我的目标是将开始时间和结束时间转换为纪元并以秒为单位计算持续时间。然后用新值输出同一个对象。
当我这样做时
echo $J_SON | jq '.Start |= (strptime("%Y-%m-%d %H:%M:%S") | mktime),.End |= (strptime("%Y-%m-%d %H:%M:%S") | mktime)'
输出为:
{
"Status": "Down",
"StatusID": "75328241",
"Start": 1489763200,
"End": "2017-03-17 15:08:37",
"Period": "1 Minutes "
}
{
"Status": "Down",
"StatusID": "75328241",
"Start": "2017-03-17 15:06:40",
"End": 1489763317,
"Period": "1 Minutes "
}
这是有道理的,手册说这是默认行为。
Assignment works a little differently in jq than in most programming
languages. jq doesn’t distinguish between references to and copies of
something - two objects or arrays are either equal or not equal,
without any further notion of being “the same object” or “not the same
object”.
If an object has two fields which are arrays, .foo and .bar, and you
append something to .foo, then .bar will not get bigger, even if
you’ve previously set .bar = .foo. If you’re used to programming in
languages like Python, Java, Ruby, Javascript, etc. then you can think
of it as though jq does a full deep copy of every object before it
does the assignment (for performance it doesn’t actually do that, but
that’s the general idea).
但我想输出纪元和秒的值。
我成功地获得了这样的持续时间:
echo $J_SON | jq '.Period = ((.End | strptime("%Y-%m-%d %H:%M:%S") | mktime)-(.Start | strptime("%Y-%m-%d %H:%M:%S") | mktime))'
输出
{
"Status": "Down",
"StatusID": "75328241",
"Start": "2017-03-17 15:06:40",
"End": "2017-03-17 15:08:37",
"Period": 117
}
问题:
我可以获得每个变量的单独值,但不确定如何将它们全部连接到一个输出对象中。
您可以像这样将更新链接在一起:
.Start |= (strptime("%Y-%m-%d %H:%M:%S") | mktime)
| .End |= (strptime("%Y-%m-%d %H:%M:%S") | mktime)
| .Period = (.End - .Start)
您好,我正在尝试在单个 "jq" 命令中执行计算,但我不知道如何引用新值。
原版JSON
{
"Status": "Down",
"StatusID": "75328241",
"Start": "2017-03-17 15:06:40",
"End": "2017-03-17 15:08:37",
"Period": "1 Minutes "
}
我的目标是将开始时间和结束时间转换为纪元并以秒为单位计算持续时间。然后用新值输出同一个对象。
当我这样做时
echo $J_SON | jq '.Start |= (strptime("%Y-%m-%d %H:%M:%S") | mktime),.End |= (strptime("%Y-%m-%d %H:%M:%S") | mktime)'
输出为:
{
"Status": "Down",
"StatusID": "75328241",
"Start": 1489763200,
"End": "2017-03-17 15:08:37",
"Period": "1 Minutes "
}
{
"Status": "Down",
"StatusID": "75328241",
"Start": "2017-03-17 15:06:40",
"End": 1489763317,
"Period": "1 Minutes "
}
这是有道理的,手册说这是默认行为。
Assignment works a little differently in jq than in most programming languages. jq doesn’t distinguish between references to and copies of something - two objects or arrays are either equal or not equal, without any further notion of being “the same object” or “not the same object”.
If an object has two fields which are arrays, .foo and .bar, and you append something to .foo, then .bar will not get bigger, even if you’ve previously set .bar = .foo. If you’re used to programming in languages like Python, Java, Ruby, Javascript, etc. then you can think of it as though jq does a full deep copy of every object before it does the assignment (for performance it doesn’t actually do that, but that’s the general idea).
但我想输出纪元和秒的值。
我成功地获得了这样的持续时间:
echo $J_SON | jq '.Period = ((.End | strptime("%Y-%m-%d %H:%M:%S") | mktime)-(.Start | strptime("%Y-%m-%d %H:%M:%S") | mktime))'
输出
{
"Status": "Down",
"StatusID": "75328241",
"Start": "2017-03-17 15:06:40",
"End": "2017-03-17 15:08:37",
"Period": 117
}
问题:
我可以获得每个变量的单独值,但不确定如何将它们全部连接到一个输出对象中。
您可以像这样将更新链接在一起:
.Start |= (strptime("%Y-%m-%d %H:%M:%S") | mktime)
| .End |= (strptime("%Y-%m-%d %H:%M:%S") | mktime)
| .Period = (.End - .Start)