无法确定如何过滤jq输出以仅显示来自json-rpc响应的结果或错误消息。示例响应如下所示:
普通:
{
"result": "0001",
"id": 1,
"jsonrpc": "2.0"
}
错误:
{
"error": {
"code": -32000,
"message": "Server error",
"data": {
"type": "TypeError",
"args": [
"'NoneType' object is not subscriptable"
],
"message": "'NoneType' object is not subscriptable"
}
},
"id": 1,
"jsonrpc": "2.0"
}
我可以单独访问它们两个,但不知道如何获得'或'功能
$ ./reqok | jq .result
"0001"
$ ./reqbad | jq .error.data.message
"'NoneType' object is not subscriptable"
在jq
中,您可以使用备用运算符//
,该运算符可用于返回默认值。例如。如果a // b
产生除a
和a
以外的结果,则形式为false
的过滤器将产生与null
相同的结果。否则,a // b
会产生与b
相同的结果。
也可用于提供默认值:如果输入中没有.foo // 1
元素,则1
的取值为.foo
。>
起作用,在这种情况下,执行也会变为备用条件。jq '.result? // .error.data.message?'
请注意,当
.result
的值不是null
或false
时,这将[[仅