0

所以我已经阅读了jq教程并使用了github json那里的响应并提取了其他一些键的一些值,所以我想我理解语法是如何工作的。不幸的是,尝试在 Googlesafe-browsing json响应中使用它时没有任何效果。以下是存储在变量中的完整响应(对于jq教程中的 github 示例也是如此):

echo "$safeb"
{
  "matches": [
{
  "threatType": "MALWARE",
  "platformType": "ALL_PLATFORMS",
  "threat": {
    "url": "http://www.wittyvideos.com"
  },
  "cacheDuration": "300s",
  "threatEntryType": "URL"
  }
 ]
}

...这就是我尝试过的:

echo "$safeb" | jq '.matches.threatType'
jq: error (at <stdin>:13): Cannot index array with string "threatType"

echo "$safeb" | jq '.threatType'
null

echo "$safeb" | jq '.[] | .threatType'
jq: error (at <stdin>:13): Cannot index array with string "threatType"

echo "$safeb" | jq '.[] | {type: .threatType}'
jq: error (at <stdin>:13): Cannot index array with string "threatType"

提前致谢。

4

1 回答 1

0

.matches.threatType

.matches是一个数组,因此您必须使用[]它来扩展它,例如:

.matches[].threatType

.threatType

这种尝试可以通过使用来挽救..,例如

.. | .threatType? // empty

.[] | .threatType

没有意见 :-)

.[] | {类型:.threatType}

你可能的意思是:

.matches[] | {type: .threatType}

ps

您可能想用来debug帮助调试/了解正在发生的事情。

另外,将来请不要忘记描述或显示预期的输出。

于 2018-03-15T13:25:31.973 回答