[英]Extract an information from a JSON file with jq and set the information in a variable
I want to extract an information from a JSON file called blablabla.json which look like this: 我想从一个名为blablabla.json的JSON文件中提取信息,如下所示:
{ "token": { "issued_at": "2018-11-04T23:35:07Z", "expires_at": "2018-11-05T00:35:07Z", "user": { "id": "ide", "name": "ide" } } }
I want to get the "expires_at" date to compare it with the current date. 我想获取“ expires_at”日期以将其与当前日期进行比较。 To do that, I use this:
为此,我使用以下代码:
type blablabla.json|jq .token".expires_at"
It works, but I don't know how to set it to a variable. 它有效,但是我不知道如何将其设置为变量。 I try somethings like:
我尝试类似的东西:
SET date=type blablabla.json|jq .token".expires_at"
but it does nothing. 但它什么也没做。 Do you have an idea?
你有想法吗?
Thank you very much. 非常感谢你。
Generally, you'd use a For /F
loop: 通常,您将使用
For /F
循环:
@Echo Off
For /F "Delims=" %%A In ('type blablabla.json^|jq .token".expires_at"'
) Do Set "Expiry=%%~A"
Where your result can be accessed using the variable, %Expiry%
. 可以使用变量
%Expiry%
访问结果的位置。 (You wouldn't set a variable to the name of an important existing system variable) . (您不会将变量设置为现有的重要系统变量的名称) 。
As for checking it against the current date, you'd need to find a method of doing that as both dates would be strings, not date objects. 至于对照当前日期进行检查,您需要找到一种方法来执行此操作,因为两个日期都是字符串,而不是日期对象。 That is outside of the scope of this question .
那超出了这个问题的范围 。
I'd use PowerShell for this (at least as a tool) for several reasons: 由于以下几个原因,我将为此使用PowerShell(至少作为一种工具):
This one line PowerShell script returns expires.at as a day diffference from today including conversion from UTC. 这一行PowerShell脚本返回的expedes.at与今天(包括UTC的转换)相差一天。
(([datetime](Get-Content .\blablabla.json |ConvertFrom-Json).token.expires_at).Date - (Get-Date).date).Days
And can easily be wrapped in a batch. 并且可以轻松地分批包装。
@Echo off
For /f %%A in ('
powershell -NoP -C "(([datetime](Get-Content .\blablabla.json |ConvertFrom-Json).token.expires_at).Date - (Get-Date).date).Days"
') Do Set "Expires=%%A"
Set Expires
This will (for today) return: 这将(今天)返回:
Expires=0
and negative values for past dates, positive values for future dates. 过去日期为负值,未来日期为正值。
Yes, I know that a .json file should not be processed via a Batch file for a large number of reasons. 是的,我知道由于多种原因, 不应通过批处理文件来处理.json文件。 However, if the problem is simple (like this one) and there are not additional constraints, then a Batch file solution could be the simplest one, like this two-lines code that includes the date comparison...
但是,如果问题很简单(如此问题)并且没有其他限制,则批处理文件解决方案可能是最简单的解决方案,例如包含日期比较的两行代码...
for /F "tokens=2 delims=, " %%a in ('findstr "expires_at" blablabla.json') do set "expires=%%~a"
if %expires:~0,4%%expires:~5,2%%expires:~8,2% geq %date:~6,4%%date:~0,2%%date:~3,2% goto expired
With Xidel it's as simple as: 使用Xidel,它非常简单:
xidel -s blablabla.json -e "$json//expires_at"
This returns its value: 2018-11-05T00:35:07Z
. 这将返回其值:
2018-11-05T00:35:07Z
。
To export this as %expdate%
: 要将其导出为
%expdate%
:
FOR /F "delims=" %%A IN ('xidel.exe -s blablabla.json -e "expdate:=$json//expires_at" --output-format=cmd') DO %%A
Xidel can also return the current date in the same format: Xidel还可以以相同格式返回当前日期:
xidel -s blablabla.json -e "$json//expires_at,current-dateTime()"
This returns: 返回:
2018-11-05T00:35:07Z
2019-01-07T18:26:33.453
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.