我想在现有文档中添加新路径
./jq < test.json
{
"correlationId": "6298865a73b477106c98d021",
"leg": 0,
"tag": "sent",
"offset": 322858,
"len": 178,
"prev": {
"page": {
"file": 10352,
"page": 2
},
"record": 911
},
"data": "HTTP/1.1 403 ForbiddenrnDate: Fri, 16 Feb 2018 08:37:54 GMTrnServer: rnConnection: closernX-CorrelationID: Id-6298865a73b477106c98d021 0rnContent-Type: text/htmlrnrnAccess Denied"
}
我正在使用过滤器 setPath JQ手册中描述的。但是即使我复制了已记录的字符串
./jq 'setpath([0,"a"]; 1)' < test.json
仍然出现错误:
jq: error (at <stdin>:1): Cannot index object with number
我看不到任何语法问题。我忽略了什么吗?
问候,谢谢,雷迪
您不能将整数索引用作JSON对象中的键。对于JSON对象,键必须是一个字符串,因此您可以写:
jq 'setpath(["0","a"]; 1)' < test.json
输出:
{
"correlationId": "6298865a73b477106c98d021",
"leg": 0,
"tag": "sent",
"offset": 322858,
"len": 178,
"prev": {
"page": {
"file": 10352,
"page": 2
},
"record": 911
},
"data": "HTTP/1.1 403 ForbiddenrnDate: Fri, 16 Feb 2018 08:37:54 GMTrnServer: rnConnection: closernX-CorrelationID: Id-6298865a73b477106c98d021 0rnContent-Type: text/htmlrnrnAccess Denied",
"0": {
"a": 1
}
}