简体   繁体   English

Bash/JQ 解析错误:第 1 行第 254 列值之间的预期分隔符

[英]Bash/JQ parse error: Expected separator between values at line 1, column 254

I'm getting a parse error: while executing the function我收到一个解析错误:在执行函数时

Is this something to do with jq or bash error?这与 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
}

sample error-log.json content示例 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-output content 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}
]

error output错误输出

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

Your data source is a well-formed JSON document, there is no issue at this level.您的数据源是格式良好的 JSON 文档,此级别没有问题。

The issue occurs when you read i each line output by your jq command.当您read i jq命令的每一行输出时,就会出现问题。 As there are escaped characters, the read command will interpret them and remove them.由于存在转义字符,读取命令将解释它们并删除它们。 Then, later inside your loop, the subsequent call to jq will consider that there are unescaped characters.然后,稍后在您的循环中,对jq的后续调用将认为存在未转义的字符。

So I fix your script to take these two points into account:因此,我修复了您的脚本以考虑这两点:

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
}

You can see that read is called with thr -r flag to:您可以看到使用 thr -r标志调用read以:

-r do not allow backslashes to escape any characters -r 不允许反斜杠转义任何字符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM