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

时间:2019-07-26 12:09:11

标签: json bash runtime-error jq

我遇到解析错误:执行函数时

这与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

对此表示感谢

1 个答案:

答案 0 :(得分:0)

您的数据源是格式良好的JSON文档,在此级别上没有问题。

当您read i命令jq输出的每一行时,都会发生此问题。由于存在转义字符,因此read命令将解释它们并将其删除。然后,在循环的后面,随后对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
}

您可以看到read的调用带有thr -r标志,用于:

  

-r不允许反斜杠转义任何字符