Docker中运行AWS RDS输出的JSON为何被工具判定为格式错误?
问题:AWS CLI输出的JSON本地工具验证失败,在线工具却判定有效
现象
我认为以下JSON格式是有效的:
{ "DescribeDBLogFiles": [ { "LogFileName": "error/postgresql.log.2022-09-14-00", "LastWritten": 1663199972348, "Size": 3032193 } ] }
A) 本地JSON解析工具均判定无效
# 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) 在线JSON验证工具判定有效
将上述JSON复制粘贴到在线验证工具中,均显示格式有效。
排查细节
根据json_pp的错误提示,执行hexdump <<< "$logfiles"后发现,$logfiles变量的内容前缀存在额外字符:5b1b 313f 1b68 0d3d 1b7b ....,其中7b才对应JSON的起始符号{。
变量来源
该JSON是通过以下命令存入logfiles变量的:
logfiles=$(aws rds describe-db-log-files \ --db-instance-identifier somedb \ --filename-contains 2022-09-14) # aws别名定义 alias aws='docker run --rm -it -v ~/.aws:/root/.aws amazon/aws-cli:2.7.31'
当前使用的bash版本:
> bash --version GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
已尝试的排查方向
已查阅相关GitHub Issue,但未找到确切原因。怀疑是使用echo输出时双引号被破坏,有用户反馈用printf可解决此类问题。
内容的提问来源于stack exchange,提问作者VanillaDonuts
相关产品推荐
相关产品推荐

