Parse JSON in cURL query

Advertisements

I’ve got JSON from cURL:

{
  "log": {
    "52911": {
      "message": {
        "level": "Info",
        "label": "I",
        "message": "DHCPREQUEST received (STATE_RENEWING) for 192.168.0.12 from b1:00:00:02:e1:2f. "
      },
      "timestamp": "Feb 10 13:06:14",
      "ident": "ndhcps",
      "id": 52911
    },
    "52912": {
      "message": {
        "level": "Info",
        "label": "I",
        "message": "sending ACK of 192.168.0.12 to b1:00:00:02:e1:2f. "
      },
      "timestamp": "Feb 10 13:06:14",
      "ident": "ndhcps",
      "id": 52912
    }
  }
}

How to filter all messages with pattern "DHCPREQUEST" in comand line?

I’d tryed | jq -r '.logs | .[].message.message | select(.!="DHCPREQUEST")', it didn’t work.

The result I need to get is: DHCPREQUEST received (STATE_RENEWING) for 192.168.0.12 from b1:00:00:02:e1:2f.

>Solution :

You can use index for testing if a string contains another.

jq -r '.log[].message.message | select(index("DHCPREQUEST"))'

Leave a Reply Cancel reply