在我看来,以下JSON看起来是有效的.
{
"DescribeDBLogFiles": [
{
"LogFileName": "error/postgresql.log.2022-09-14-00",
"LastWritten": 1663199972348,
"Size": 3032193
}
]
}
A) But, jq
、json_pp
和Pythonjson.tool
模块将其视为invalid:
# jq 1.6
> echo "$logfiles" | jq
parse error: Invalid numeric literal at line 1, column 2
# json_pp 4.02
> echo "$logfiles" | json_pp
malformed JSON string, neither array, object, number, string or atom,
at character offset 0 (before "\x{1b}[?1h\x{1b}=\r{...") at /usr/bin/json_pp line 51
> python3 -m json.tool <<< "$logfiles"
Expecting value: line 1 column 1 (char 0)
B) But on the other hand,如果将上述JSON复制粘贴到在线验证器(1和2)中,则将其视为valid.
正如上面json_pp
的错误所暗示的,hexdump <<< "$logfiles"
确实显示了附加的、周围的字符.前缀是:5b1b 313f 1b68 0d3d 1b7b ....
,其中7b
是{
.
以下命令将JSON输出为logfiles
变量:
logfiles=$(aws rds describe-db-log-files \
--db-instance-identifier somedb \
--filename-contains 2022-09-14)
# where `aws` is
alias aws='docker run --rm -it -v ~/.aws:/root/.aws amazon/aws-cli:2.7.31'
> bash --version
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
我已经细读了this GitHub issue个,但还是找不出原因.我怀疑,在使用echo
时,双引号不知何故被 destruct 了--一些人报告说,printf
对他们来说是"有效的".