我想在现有文档中添加新路径
./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 Forbidden\r\nDate: Fri, 16 Feb 2018 08:37:54 GMT\r\nServer: \r\nConnection: close\r\nX-CorrelationID: Id-6298865a73b477106c98d021 0\r\nContent-Type: text/html\r\n\r\nAccess Denied"
}
我正在使用jq手册中描述的过滤器 setpath 。 但即使我复制了记录的字符串
./jq 'setpath([0,"a"]; 1)' < test.json
仍然收到错误:
jq: error (at <stdin>:1): Cannot index object with number
我没有看到任何语法问题。我忽略了什么吗?
关心并感谢,Reddy
答案 0 :(得分:2)
您不能将整数索引用作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 Forbidden\r\nDate: Fri, 16 Feb 2018 08:37:54 GMT\r\nServer: \r\nConnection: close\r\nX-CorrelationID: Id-6298865a73b477106c98d021 0\r\nContent-Type: text/html\r\n\r\nAccess Denied",
"0": {
"a": 1
}
}