jq报错:parse error: Invalid numeric literal at line原因分析

jq报错:parse error: Invalid numeric literal at line原因分析

在shell中使用jq对json进行解析,但是发现:

pods_params={"id":423,"workId":4,"imageIds":["aa5e0d05330c43918630100"]}
e=$( echo "$pods_params"| jq -r ".id" )
echo $e

结果提示:

parse error: Invalid numeric literal at line 1, column 4
或者:is not defined at <top-level> 

以为是jq语法的原因,弄了半天还是报错。查询后找不出问题。看了别人的例子才发现,要在json文件外边加上单引号!!!

pods_params='{"id":423,"workId":4,"imageIds":["aa5e0d05330c43918630100"]}'
e=$( echo "$pods_params"| jq -r ".id" )
echo $e

直接从文件夹读取json文件就么有这个问题!!

jq报错:jq: error: syntax error, unexpected IDENT, expecting $end (Unix shell quoting issues?) at ,原因分析!

后来在向l_results.json文件中写入字符串“Ture:Map is ok!”:

totalResultJson="Ture:Map is ok!"
addData=$(cat /tmp/scripts/l_results.json | jq ".imageId.data=$totalResultJson" )

发现上边的报错问题。一直提示错误:

jq: error: syntax error, unexpected ':', expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
或者
jq: error: syntax error, unexpected IDENT, expecting $end (Unix shell quoting issues?) at <top-level>
或者:
jq: error: totalResultJson/0 is not defined at <top-level>
jq: 1 compile error

开始以为是字符串中的或者的问题,但是删除后发现还是有问题。说明不是这个问题。
把totalResultJson=1.也不会报错,说明是针对字符串的语法问题。
接下来实验,把字符串加上单引号:

totalResultJson='"Ture:Map is ok!"'

然后就ok了,就不会报错。
不过如果totalResultJson是从文件读取出来的变量,就没法使用这个方法,直接在jq上加单引号

addData=$(cat /tmp/scripts/l_results.json | jq ".imageId.data='$totalResultJson'" )

会直接导致变量$totalResultJson无法解析出来。

所以最后的办法是字符串拼接,将字符串拼接上双引号,就可以实现。

# 获取字符串,写入变量
totalResult=$(cat ./map_test/result.txt)
# 将字符串拼接双引号
totalResultJson=\"${totalResult}\"
# 执行jq写入
addData=$(cat /tmp/scripts/l_results.json | jq ".imageId.data='$totalResultJson'" )

最后成功将字符串写入json。

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值