当值不是对象时无法处理 jq 中的错误
Unable to handle error in jq when value is not object
我正在尝试简化 "try-catch" 以检查“.data”元素的值是否包含下一个对象或简单字符串。最重要的是要消除报告给标准输出的错误(其他方式然后通用重定向到 /dev/null)
我目前正在使用这个:
json2=`$APPFOLDER/jq -c '.data |= fromjson' <<< $json`
if [[ ! $json2 ]]
then
json2=`$APPFOLDER/jq -c '.data |= { text: .}' <<< $json`
json2=`$APPFOLDER/jq -c '.data |= { message: .}' <<< $json2`
fi
为了将最终的简单字符串移动到 .data.message.text 元素
但是没有更简单的方法吗?
当然它会向标准输出报告错误,例如
jq: error (at <stdin>:1): Invalid numeric literal at line 1, column 9 (while parsing 'HTTP/1.1 403 Forbidden
Date: Tue, 27 Feb 2018 08:13:32 GMT
Server:
Connection: close
X-CorrelationID: Id-2c13955ae3bb6c3cc943460b 0
Content-Type: text/html
Access Denied')
我想试试
jq -r 'try .data |= fromjson catch "STRING"'
但它给我错误:
jq: error: syntax error, unexpected catch, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
try .data |= fromjson catch "STRING"
jq: 1 compile error
exit status 3
这是示例消息:
{"correlationId":"2c13955ae3bb6c3cc943460b","leg":0,"tag":"sent","offset":167408,"len":178,"prev":{"page":{"file":10481,"page":2},"record":1736},"data":"HTTP/1.1
403 Forbidden\r\nDate: Tue, 27 Feb 2018 08:13:32 GMT\r\nServer:
\r\nConnection: close\r\nX-CorrelationID: Id-2c13955ae3bb6c3cc943460b
0\r\nContent-Type: text/html\r\n\r\nAccess Denied"}
I wanted to try
jq -r 'try .data |= fromjson catch "STRING"'
这是您必须使用括号来帮助解析器的情况之一:
jq -r 'try (.data |= fromjson) catch "STRING"'
我正在尝试简化 "try-catch" 以检查“.data”元素的值是否包含下一个对象或简单字符串。最重要的是要消除报告给标准输出的错误(其他方式然后通用重定向到 /dev/null)
我目前正在使用这个:
json2=`$APPFOLDER/jq -c '.data |= fromjson' <<< $json`
if [[ ! $json2 ]]
then
json2=`$APPFOLDER/jq -c '.data |= { text: .}' <<< $json`
json2=`$APPFOLDER/jq -c '.data |= { message: .}' <<< $json2`
fi
为了将最终的简单字符串移动到 .data.message.text 元素
但是没有更简单的方法吗? 当然它会向标准输出报告错误,例如
jq: error (at <stdin>:1): Invalid numeric literal at line 1, column 9 (while parsing 'HTTP/1.1 403 Forbidden
Date: Tue, 27 Feb 2018 08:13:32 GMT
Server:
Connection: close
X-CorrelationID: Id-2c13955ae3bb6c3cc943460b 0
Content-Type: text/html
Access Denied')
我想试试
jq -r 'try .data |= fromjson catch "STRING"'
但它给我错误:
jq: error: syntax error, unexpected catch, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
try .data |= fromjson catch "STRING"
jq: 1 compile error
exit status 3
这是示例消息:
{"correlationId":"2c13955ae3bb6c3cc943460b","leg":0,"tag":"sent","offset":167408,"len":178,"prev":{"page":{"file":10481,"page":2},"record":1736},"data":"HTTP/1.1 403 Forbidden\r\nDate: Tue, 27 Feb 2018 08:13:32 GMT\r\nServer: \r\nConnection: close\r\nX-CorrelationID: Id-2c13955ae3bb6c3cc943460b 0\r\nContent-Type: text/html\r\n\r\nAccess Denied"}
I wanted to try
jq -r 'try .data |= fromjson catch "STRING"'
这是您必须使用括号来帮助解析器的情况之一:
jq -r 'try (.data |= fromjson) catch "STRING"'