Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用shell jq动态解析JSON对象_Json_Linux_Bash_Shell_Jq - Fatal编程技术网

如何使用shell jq动态解析JSON对象

如何使用shell jq动态解析JSON对象,json,linux,bash,shell,jq,Json,Linux,Bash,Shell,Jq,我有一个关于壳牌jq的问题。因此,我的JSON对象是: {"1543446000": {"name": "John", "company": "foo"}, "1543359600": {"name": "Kate", "company": "bar"}} 数字1543446000和1543359600是UNIX时间戳。如何使用shell变量通过时间戳解析其中一个JSON对象 到目前为止,我的shell脚本: #!/bin/sh URL="https://pastebin.com/raw/w7

我有一个关于壳牌jq的问题。因此,我的JSON对象是:

{"1543446000": {"name": "John", "company": "foo"}, "1543359600": {"name": "Kate", "company": "bar"}}
数字
1543446000
1543359600
是UNIX时间戳。如何使用shell变量通过时间戳解析其中一个JSON对象

到目前为止,我的shell脚本:

#!/bin/sh
URL="https://pastebin.com/raw/w7awz7kZ"
export DATE=$(date -d "$today 0" +%s)
JSON=$(curl -H "Accept: application/json" $API_URL)
JSON=$(echo $JSON | jq --arg date $DATE '.$date')
echo $JSON
似乎不起作用。我的意图是选择一个时间戳描述的内部JSON对象,这基本上是今天午夜。所以我想选择今天的数据集

有什么建议吗

问候,
Innoberger

您需要使用密钥访问的完整语法,因为美元符号不允许您使用较短的形式。错误消息应提供此建议

$ jq --arg date 1543359600 '.$date' tmp.json
jq: error: syntax error, unexpected '$' (Unix shell quoting issues?) at <top-level>, line 1:
.$date
jq: error: try .["field"] instead of .field for unusually named fields at <top-level>, line 1:
.$date
jq: 2 compile errors
$ jq --arg date 1543359600 '.[$date]' tmp.json
{
  "name": "Kate",
  "company": "bar"
}