Search code examples
jsonposixjq

How to move value of JSON element into subelement using bash


I have an interesting problem. I have this document:

{
  "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"
}

But because the "data" element could contain also regular JSON object I would like to move the value

"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"

into "data": { "message": "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"} structure.

I tried to find a solution with jq but I didn't found any filter which would move it.

Any idea please?


Solution

  • It seems you want:

    .data |= { message: .}