Home > Software engineering >  One jq command to get expected output
One jq command to get expected output

Time:01-26

I want to format below json code and print only specific values.

I tried using below command and got the below json output. But when I further try to parse the code for end result it didn't worked as expected.

So, a little help can make things work for me.

Command that results in below JSON code output:

mgmt_cli -r true -d 192.168.86.201 show access-rulebase name Network --format json | jq --raw-output ' .rulebase[].rulebase[] | {name: .name, rule_number: ."rule-number"}'

JSON Code:

{
  "name": "MgmtRule",
  "rule_number": 1
}
{
  "name": null,
  "rule_number": 2
}
{
  "name": null,
  "rule_number": 3
}
{
  "name": "Inside Rules",
  "rule_number": 4
}
{
  "name": null,
  "rule_number": 5
}
{
  "name": null,
  "rule_number": 6
}
{
  "name": null,
  "rule_number": 7
}
{
  "name": null,
  "rule_number": 8
}
{
  "name": null,
  "rule_number": 9
}
{
  "name": "Cleanup rule",
  "rule_number": 10
}

Further formatting jq command that I added to the above command to get the end result:

jq --slurp '"\(.[0])-\(.[length -1])"'

Unexpected Output:

"{\"name\":\"MgmtRule\",\"rule_number\":1}-{\"name\":\"Cleanup rule\",\"rule_number\":10}"

Expected Output:

MgmtRule (1-3)
Inside Rules (4-9)
Cleanup rule (10)

Raw output:

Short form of original raw output:

{
  "uid" : "38271c2f-ab44-4e25-9aa4-e219cb6e12cf",
  "name" : "Network",
  "rulebase" : [ {
    "uid" : "38acf747-85ee-4962-a3e7-d3cd678c388f",
    "name" : "Internal Rules",
    "type" : "access-section",
    "from" : 1,
    "to" : 3,
    "rulebase" : [ {
      "uid" : "bdd9d868-51b2-4210-8c37-f0237cd560fa",
      "name" : "Test",
      "type" : "access-rule",
      "domain" : {
        "uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
        "name" : "SMC User",
        "domain-type" : "domain"
      },
      "rule-number" : 1,
      "track" : {
        "type" : "598ead32-aa42-4615-90ed-f51a5928d41d",
        "per-session" : false,
        "per-connection" : true,
        "accounting" : false,
        "enable-firewall-session" : false,
        "alert" : "none"
      },
      "source" : [ "6ddb84da-ef72-45c8-b2c4-bb3a849a6aa9" ],
      "source-negate" : false,
      "destination" : [ "375f4a15-1a7d-0b48-8ffc-2e9eb4e512ae" ],
      "destination-negate" : false,
      "service" : [ "97aeb443-9aea-11d5-bd16-0090272ccb30", "18ec9eaa-1657-4240-ab97-5f234623336b" ],
      "service-negate" : false,
      "service-resource" : "",
      "vpn" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
      "action" : "6c488338-8eec-4103-ad21-cd461ac2c472",
      "action-settings" : {
        "enable-identity-captive-portal" : false
      },
      "content" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
      "content-negate" : false,
      "content-direction" : "any",
      "time" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
      "custom-fields" : {
        "field-1" : "",
        "field-2" : "",
        "field-3" : ""
      },
      "meta-info" : {
        "lock" : "unlocked",
        "validation-state" : "ok",
        "last-modify-time" : {
          "posix" : 1674486777583,
          "iso-8601" : "2023-01-23T20:42 0530"
        },
        "last-modifier" : "admin",
        "creation-time" : {
          "posix" : 1674486722309,
          "iso-8601" : "2023-01-23T20:42 0530"
        },
        "creator" : "admin"
      },
      "comments" : "",
      "enabled" : true,
      "install-on" : [ "6c488338-8eec-4103-ad21-cd461ac2c476" ],
      "available-actions" : {
        "edit" : "true",
        "delete" : "true",
        "clone" : "not_supported"
      }
    }, {
      "uid" : "bb93b38a-c672-49ec-afb0-8a90e8518437",
      "type" : "access-rule",
      "domain" : {
        "uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
        "name" : "SMC User",
        "domain-type" : "domain"
      },
      "rule-number" : 2,
      "track" : {
        "type" : "598ead32-aa42-4615-90ed-f51a5928d41d",
        "per-session" : false,
        "per-connection" : true,
        "accounting" : false,
        "enable-firewall-session" : false,
        "alert" : "none"
      },
      "source" : [ "6ddb84da-ef72-45c8-b2c4-bb3a849a6aa9" ],
      "source-negate" : false,
      "destination" : [ "8dbb91f3-786b-4d11-a029-45ee5b7d59cf" ],
      "destination-negate" : false,
      "service" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
      "service-negate" : false,
      "service-resource" : "",
      "vpn" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
      "action" : "6c488338-8eec-4103-ad21-cd461ac2c472",
      "action-settings" : {
        "enable-identity-captive-portal" : false
      },
      "content" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
      "content-negate" : false,
      "content-direction" : "any",
      "time" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
      "custom-fields" : {
        "field-1" : "",
        "field-2" : "",
        "field-3" : ""
      },
      "meta-info" : {
        "lock" : "unlocked",
        "validation-state" : "ok",
        "last-modify-time" : {
          "posix" : 1674488952100,
          "iso-8601" : "2023-01-23T21:19 0530"
        },
        "last-modifier" : "admin",
        "creation-time" : {
          "posix" : 1674488917458,
          "iso-8601" : "2023-01-23T21:18 0530"
        },
        "creator" : "admin"
      },
      "comments" : "",
      "enabled" : true,
      "install-on" : [ "6c488338-8eec-4103-ad21-cd461ac2c476" ],
      "available-actions" : {
        "edit" : "true",
        "delete" : "true",
        "clone" : "not_supported"
      }
    }, {
      "uid" : "925f210e-ce4f-4139-870e-3cd53f46d832",
      "type" : "access-rule",
      "domain" : {
        "uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
        "name" : "SMC User",
        "domain-type" : "domain"
      },
      "rule-number" : 3,
      "track" : {
        "type" : "598ead32-aa42-4615-90ed-f51a5928d41d",
        "per-session" : false,
        "per-connection" : true,
        "accounting" : false,
        "enable-firewall-session" : false,
        "alert" : "none"
      },
      "source" : [ "6ddb84da-ef72-45c8-b2c4-bb3a849a6aa9" ],
      "source-negate" : false,
      "destination" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
      "destination-negate" : false,
      "service" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
      "service-negate" : false,
      "service-resource" : "",
      "vpn" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
      "action" : "6c488338-8eec-4103-ad21-cd461ac2c473",
      "action-settings" : { },
      "content" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
      "content-negate" : false,
      "content-direction" : "any",
      "time" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
      "custom-fields" : {
        "field-1" : "",
        "field-2" : "",
        "field-3" : ""
      },
      "meta-info" : {
        "lock" : "unlocked",
        "validation-state" : "ok",
        "last-modify-time" : {
          "posix" : 1674490197006,
          "iso-8601" : "2023-01-23T21:39 0530"
        },
        "last-modifier" : "admin",
        "creation-time" : {
          "posix" : 1674490169763,
          "iso-8601" : "2023-01-23T21:39 0530"
        },
        "creator" : "admin"
      },
      "comments" : "",
      "enabled" : true,
      "install-on" : [ "6c488338-8eec-4103-ad21-cd461ac2c476" ],
      "available-actions" : {
        "edit" : "true",
        "delete" : "true",
        "clone" : "not_supported"
      }
    } ]
  }, {
    "uid" : "da0fdbf6-360b-44a4-94a8-b01894d3b5a0",
    "name" : "cleanup rule",
    "type" : "access-section",
    "from" : 4,
    "to" : 4,
    "rulebase" : [ {
      "uid" : "2b922948-da96-4c9d-a654-063e0183f9ae",
      "name" : "Cleanup rule",
      "type" : "access-rule",
      "domain" : {
        "uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
        "name" : "SMC User",
        "domain-type" : "domain"
      },
      "rule-number" : 4,
      "track" : {
        "type" : "598ead32-aa42-4615-90ed-f51a5928d41d",
        "per-session" : false,
        "per-connection" : true,
        "accounting" : false,
        "enable-firewall-session" : false,
        "alert" : "none"
      },
      "source" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
      "source-negate" : false,
      "destination" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
      "destination-negate" : false,
      "service" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
      "service-negate" : false,
      "service-resource" : "",
      "vpn" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
      "action" : "6c488338-8eec-4103-ad21-cd461ac2c473",
      "action-settings" : { },
      "content" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
      "content-negate" : false,
      "content-direction" : "any",
      "time" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
      "custom-fields" : {
        "field-1" : "",
        "field-2" : "",
        "field-3" : ""
      },
      "meta-info" : {
        "lock" : "unlocked",
        "validation-state" : "ok",
        "last-modify-time" : {
          "posix" : 1674486751969,
          "iso-8601" : "2023-01-23T20:42 0530"
        },
        "last-modifier" : "admin",
        "creation-time" : {
          "posix" : 1668629634218,
          "iso-8601" : "2022-11-17T01:43 0530"
        },
        "creator" : "System"
      },
      "comments" : "",
      "enabled" : true,
      "install-on" : [ "6c488338-8eec-4103-ad21-cd461ac2c476" ],
      "available-actions" : {
        "edit" : "true",
        "delete" : "true",
        "clone" : "not_supported"
      }
    } ]
  } ],
  "objects-dictionary" : [ {
    "uid" : "6c488338-8eec-4103-ad21-cd461ac2c472",
    "name" : "Accept",
    "type" : "RulebaseAction",
    "domain" : {
      "uid" : "a0bbbc99-adef-4ef8-bb6d-defdefdefdef",
      "name" : "Check Point Data",
      "domain-type" : "data domain"
    },
    "icon" : "Actions/actionsAccept",
    "color" : "none"
  }, {
    "uid" : "8dbb91f3-786b-4d11-a029-45ee5b7d59cf",
    "name" : "All_Internet",
    "type" : "address-range",
    "domain" : {
      "uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
      "name" : "SMC User",
      "domain-type" : "domain"
    },
    "ipv4-address-first" : "0.0.0.0",
    "ipv4-address-last" : "255.255.255.255",
    "icon" : "Objects/ip",
    "color" : "sienna"
  }, {
    "uid" : "97aeb369-9aea-11d5-bd16-0090272ccb30",
    "name" : "Any",
    "type" : "CpmiAnyObject",
    "domain" : {
      "uid" : "a0bbbc99-adef-4ef8-bb6d-defdefdefdef",
      "name" : "Check Point Data",
      "domain-type" : "data domain"
    },
    "icon" : "General/globalsAny",
    "color" : "black"
  }, {
    "uid" : "6c488338-8eec-4103-ad21-cd461ac2c473",
    "name" : "Drop",
    "type" : "RulebaseAction",
    "domain" : {
      "uid" : "a0bbbc99-adef-4ef8-bb6d-defdefdefdef",
      "name" : "Check Point Data",
      "domain-type" : "data domain"
    },
    "icon" : "Actions/actionsDrop",
    "color" : "none"
  }, {
    "uid" : "375f4a15-1a7d-0b48-8ffc-2e9eb4e512ae",
    "name" : "dummy",
    "type" : "simple-gateway",
    "domain" : {
      "uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
      "name" : "SMC User",
      "domain-type" : "domain"
    },
    "icon" : "NetworkObjects/management",
    "color" : "black"
  }, {
    "uid" : "6ddb84da-ef72-45c8-b2c4-bb3a849a6aa9",
    "name" : "H_192.168.86.111",
    "type" : "host",
    "domain" : {
      "uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
      "name" : "SMC User",
      "domain-type" : "domain"
    },
    "ipv4-address" : "192.168.86.111",
    "icon" : "Objects/host",
    "color" : "black"
  }, {
    "uid" : "97aeb443-9aea-11d5-bd16-0090272ccb30",
    "name" : "https",
    "type" : "service-tcp",
    "domain" : {
      "uid" : "a0bbbc99-adef-4ef8-bb6d-defdefdefdef",
      "name" : "Check Point Data",
      "domain-type" : "data domain"
    },
    "port" : "443",
    "icon" : "Protocols/HTTP",
    "color" : "red"
  }, {
    "uid" : "598ead32-aa42-4615-90ed-f51a5928d41d",
    "name" : "Log",
    "type" : "Track",
    "domain" : {
      "uid" : "a0bbbc99-adef-4ef8-bb6d-defdefdefdef",
      "name" : "Check Point Data",
      "domain-type" : "data domain"
    },
    "icon" : "Track/tracksLog",
    "color" : "none"
  }, {
    "uid" : "6c488338-8eec-4103-ad21-cd461ac2c476",
    "name" : "Policy Targets",
    "type" : "Global",
    "domain" : {
      "uid" : "a0bbbc99-adef-4ef8-bb6d-defdefdefdef",
      "name" : "Check Point Data",
      "domain-type" : "data domain"
    },
    "icon" : "General/globalsAny",
    "color" : "none"
  }, {
    "uid" : "18ec9eaa-1657-4240-ab97-5f234623336b",
    "name" : "ssh",
    "type" : "service-tcp",
    "domain" : {
      "uid" : "a0bbbc99-adef-4ef8-bb6d-defdefdefdef",
      "name" : "Check Point Data",
      "domain-type" : "data domain"
    },
    "port" : "22",
    "icon" : "Services/TCPService",
    "color" : "red"
  } ],
  "from" : 1,
  "to" : 4,
  "total" : 4
}

Elements that I'm trying to fetch and get the end result from above raw output:

"name" & "rule-number"

Expected Output from above raw data:

Internal Rules (1-3)
cleanup rule (4)

CodePudding user response:

By transforming the unprocessed, raw output generated by mgmt_cli, the solution becomes almost trivial since the JSON already contains the "to" and "from" properties.

Input (shortened to the relevant bits):

{
  "uid": "38271c2f-ab44-4e25-9aa4-e219cb6e12cf",
  "name": "Network",
  "rulebase": [
    {
      "uid": "38acf747-85ee-4962-a3e7-d3cd678c388f",
      "name": "Internal Rules",
      "type": "access-section",
      "from": 1,
      "to": 3
    },
    {
      "uid": "da0fdbf6-360b-44a4-94a8-b01894d3b5a0",
      "name": "cleanup rule",
      "type": "access-section",
      "from": 4,
      "to": 4
    }
  ],
  "from": 1,
  "to": 4,
  "total": 4
}

Jq transformation:

... | jq -r '.rulebase[]
| if .from == .to then "\(.name) (\(.from))"
else "\(.name) (\(.from)-\(.to))"
end'

Output:

Internal Rules (1-3)
cleanup rule (4)

Or perhaps (but I find the if/then more straightforward):

.rulebase[]
| (select(.from == .to) | "\(.name) (\(.from)-\(.to))")
// "\(.name) (\(.from)"

CodePudding user response:

Combine both filters so you don't have to --slurp anymore:

mgmt_cli … --format json | jq -r '
  reduce(.rulebase[].rulebase[] | [.name, ."rule-number"]) as [$name, $rnum] ([];
    select($name == null) // (.   [[$name, [$rnum]]])
    | last[-1] |= (select(. == [$rnum]) // .[1] = $rnum)
  )
  | .[] | "\(first) (\(last | join("-")))"
'
MgmtRule (1-3)
Inside Rules (4-9)
Cleanup rule (10)
  • Related