Thursday, July 28, 2022

[SOLVED] Grep CURL JSON Output

Issue

My curl output is

{
    "router": {
        "node_id": "ip-1-2-3-4",
        "state": "HEALTHY",
        "message": "OK"
    },

I want to get the output as

state:HEALTHY

How can i get that i'm trying

curl -sk https://localhost/router/api/v1/system/heealth | grep -Eo router.*\n.*\n.*\"state\": \"HEALTHY\"

Solution

use jq to parse JSON, way easier than grep:

curl -sk https://localhost/router/api/v1/system/heealth|jq -r .router.state


Answered By - Ron
Answer Checked By - David Marino (WPSolving Volunteer)