Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq - setpath - Cannot index object with number

Tags:

json

path

jq

I would like to add new path into existing document

./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"
}

I am using filter setpath described in jq manual. But even if I copied the documented string

./jq 'setpath([0,"a"]; 1)'  < test.json

still getting error:

jq: error (at <stdin>:1): Cannot index object with number

I do not see there any syntax issue. Did I overlooked something?

Regards and thanks, Reddy

like image 442
Reddy SK Avatar asked Oct 20 '25 17:10

Reddy SK


1 Answers

You cannot use integer indices as keys in the JSON object. For JSON objects, the key must be a string, so you could write:

jq 'setpath(["0","a"]; 1)'  < test.json

Output:

{
  "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
  }
}
like image 194
Devstr Avatar answered Oct 22 '25 23:10

Devstr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!