Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Parse JSON in cURL query

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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"))'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading