json 一个jq命令可获得预期输出

icnyk63a  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(147)

我想格式化下面的JSON代码,只打印特定的值。
我试着使用下面的命令,得到了下面的json输出,但是当我进一步尝试解析代码以得到最终结果时,它并没有像预期的那样工作。
所以,一点小小的帮助就能让事情顺利进行。

    • 产生以下JSON代码输出的命令:**
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代码:**
{
  "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
}
    • 进一步格式化jq命令,我将其添加到上述命令中以获得最终结果:**
jq --slurp '"\(.[0])-\(.[length -1])"'
    • 意外输出:**
"{\"name\":\"MgmtRule\",\"rule_number\":1}-{\"name\":\"Cleanup rule\",\"rule_number\":10}"
    • 预期产出:**
MgmtRule (1-3)
Inside Rules (4-9)
Cleanup rule (10)
    • 原始输出:**

原始输出的缩写形式:

{
  "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
}
    • 我试图从上述原始输出中获取并获得最终结果的元素:**
"name" & "rule-number"
    • 上述原始数据的预期输出:**
Internal Rules (1-3)
cleanup rule (4)
mfpqipee

mfpqipee1#

通过转换由mgmt_cli生成的未经处理的原始输出,解决方案几乎变得微不足道,因为JSON已经包含了"to"和"from"属性。
输入(缩短为相关位):

{
  "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转换:

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

输出:

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

或者也许(但我觉得if/then更直接):

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

相关问题