我收到解析错误:执行函数时
这与 jq 或 bash 错误有关吗?
generate_readable_output() {
mkdir -p smoke-test-logs/tmp
counter=1
error_length=`jq length smoke-test-logs/error-log.json`
echo "[" > smoke-test-logs/tmp/filds-output
jq -c '.[]' smoke-test-logs/error-log.json | while read i; do
msg=$(echo $i | jq -r '.msg')
type=$(echo $i | jq -r '.name')
echo "{\"title\": \"$type\",\"value\": \"$msg\",\"short\": false}," >> smoke-test-logs/tmp/filds-output;
if [ $counter -eq 20 ]
then
break
fi ;
counter=$(expr $counter + 1)
done
if [ $error_length -gt 20 ]
then echo "{\"title\": \"There are more...\",\"value\": \"There are $error_length issues need to take necessary actions immediately.\",\"short\": false}," >> smoke-test-logs/tmp/filds-output
fi ;
echo $(sed '$ s/.$//' smoke-test-logs/tmp/filds-output) > smoke-test-logs/tmp/filds-output
echo "]" >> smoke-test-logs/tmp/filds-output
}
示例 error-log.json 内容
[{"name":"single_schedule","hostname":"ip-172-31-23-92","pid":22408,"level":50,"msg":"Exception found while processing : Property address: Unit 43, 8-14 Fullerton Street, Woollahra Nsw 2025 Property Id: 95 Lease Id: 29 ErrorCode: 400 ErrorMessage 400 - \"[{\\\"errorCode\\\":400002,\\\"message\\\":\\\"Field Validation Error\\\",\\\"details\\\":\\\"Missing mandatory field dbc.\\\",\\\"type\\\":\\\"REQUEST_ERROR\\\",\\\"field\\\":\\\"dbc\\\"}]\"","time":"2019-07-26T10:57:02.079Z","v":0},
{"name":"single_schedule","hostname":"ip-172-31-23-92","pid":22408,"level":50,"msg":"Exception found while processing : Property address: 8 Chunooma Road, North Wahroonga Nsw 2076 Property Id: 96 Lease Id: 30 ErrorCode: 400 ErrorMessage 400 - \"[{\\\"errorCode\\\":400002,\\\"message\\\":\\\"Field Validation Error\\\",\\\"details\\\":\\\"Missing mandatory field dbc.\\\",\\\"type\\\":\\\"REQUEST_ERROR\\\",\\\"field\\\":\\\"dbc\\\"}]\"","time":"2019-07-26T10:57:03.287Z","v":0},
{"name":"single_schedule","hostname":"ip-172-31-23-92","pid":22408,"level":50,"msg":"Exception found while processing : Property address: Unit 17, 92 Parraween Street, Cremorne Nsw 2090 Property Id: 111 Lease Id: 38 ErrorCode: 400 ErrorMessage 400 - \"[{\\\"errorCode\\\":400002,\\\"message\\\":\\\"Field Validation Error\\\",\\\"details\\\":\\\"Missing mandatory field dbc.\\\",\\\"type\\\":\\\"REQUEST_ERROR\\\",\\\"field\\\":\\\"dbc\\\"}]\"","time":"2019-07-26T10:57:05.402Z","v":0},
{"name":"single_schedule","hostname":"ip-172-31-23-92","pid":22408,"level":50,"msg":"Exception found while processing : Property address: Unit 72, 1-3 Delmar Parade, Dee Why Nsw 2099 Property Id: 112 Lease Id: 41 ErrorCode: 400 ErrorMessage 400 - \"[{\\\"errorCode\\\":400002,\\\"message\\\":\\\"Field Validation Error\\\",\\\"details\\\":\\\"Missing mandatory field dbc.\\\",\\\"type\\\":\\\"REQUEST_ERROR\\\",\\\"field\\\":\\\"dbc\\\"}]\"","time":"2019-07-26T10:57:07.500Z","v":0}]
filds-输出内容
[ {"title": "","value": "","short": false}, {"title": "","value": "","short": false}, {"title": "","value": "","short": false}, {"title": "","value": "","short": false}, {"title": "","value": "","short": false}, {"title": "","value": "","short": false}, {"title": "","value": "","short": false}, {"title": "","value": "","short": false}, {"title": "","value": "","short": false}, {"title": "","value": "","short": false}, {"title": "","value": "","short": false}, {"title": "","value": "","short": false}, {"title": "","value": "","short": false}, {"title": "","value": "","short": false}, {"title": "","value": "","short": false}, {"title": "","value": "","short": false}, {"title": "","value": "","short": false}, {"title": "","value": "","short": false}, {"title": "","value": "","short": false}, {"title": "","value": "","short": false}, {"title": "There are more...","value": "There are 348 issues need to take necessary actions immediately.","short": false}
]
错误输出
parse error: Expected separator between values at line 1, column 254
parse error: Expected separator between values at line 1, column 254
parse error: Expected separator between values at line 1, column 245
parse error: Expected separator between values at line 1, column 245
最佳答案
您的数据源是格式良好的 JSON 文档,此级别没有问题。
当您读取
jq 命令输出的每一行时,就会出现此问题。由于存在转义字符,读取命令将解释它们并删除它们。然后,稍后在循环内,对 jq
的后续调用将认为存在未转义的字符。
因此,我修复了您的脚本以考虑这两点:
generate_readable_output() {
mkdir -p smoke-test-logs/tmp
local counter=1
local error_length=$(jq length smoke-test-logs/error-log.json)
echo "[" > smoke-test-logs/tmp/filds-output
jq -c '.[]' smoke-test-logs/error-log.json | while read -r i
do
local msg=$(echo $i | jq '.msg')
local type=$(echo $i | jq '.name')
echo "{\"title\": $type,\"value\": $msg,\"short\": false}," >> smoke-test-logs/tmp/filds-output
if [ $counter -eq 20 ]
then
break
fi
counter=$(expr $counter + 1)
done
if [ $error_length -gt 20 ]
then
echo "{\"title\": \"There are more...\",\"value\": \"There are $error_length issues need to take necessary actions immediately.\",\"short\": false}," >> smoke-test-logs/tmp/filds-output
fi
echo $(sed '$ s/.$//' smoke-test-logs/tmp/filds-output) > smoke-test-logs/tmp/filds-output
echo "]" >> smoke-test-logs/tmp/filds-output
}
您可以看到使用 thr -r
标志调用 read
来:
-r do not allow backslashes to escape any characters
关于json - Bash/JQ 解析错误 : Expected separator between values at line 1, 第 254 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57219782/