使用 PowerShell 解析 json
Parsing json with PowerShell
我尝试从 check_mk with http-api "action=get_all_hosts"
获取我所有的主机
响应是 json
格式,看起来像这样:
"{"result": {"some host name": {"attributes": {"tag_Chassis": "Vm", "tag_ServerFamily": "WindowsServer", "tag_criticality": "prod", "tag_Application": "AllApp", "alias": "some alias", "ipaddress": "172.21.x.x", "tag_networking": "lan"}, "hostname": "some host name", "path": "windows"}}"
现在我尝试格式化响应但没有成功。如何将结果格式化为具有所有属性的 table
?
您粘贴的 JSON 不正确。它不应该在开头或结尾有引号。最后还漏了一个 }
。您可以使用任何在线工具验证它 like this.
一旦你有了正确的 JSON 应该是:
{"result": {"some host name": {"attributes": {"tag_Chassis": "Vm", "tag_ServerFamily": "WindowsServer", "tag_criticality": "prod", "tag_Application": "AllApp", "alias": "some alias", "ipaddress": "172.21.x.x", "tag_networking": "lan"}, "hostname": "some host name", "path": "windows"}}}
您可以在从 JSON:
转换属性后访问它们
# Convert and save to variable
$convertedJSON = @"
{"result": {"some host name": {"attributes": {"tag_Chassis": "Vm", "tag_ServerFamily": "WindowsServer", "tag_criticality": "prod", "tag_Application": "AllApp", "alias": "some alias", "ipaddress": "172.21.x.x", "tag_networking": "lan"}, "hostname": "some host name", "path": "windows"}}}
"@ | ConvertFrom-Json
# Access attributes
$convertedJSON.result.'some host name'.attributes
# If you don't know the hostname you can find it like this
($convertedJSON.result |Get-Member | ? MemberType -eq "NoteProperty").Name
# List all attributes from your JSON
$convertedJSON.result.$(($convertedJSON.result |Get-Member | ? MemberType -eq "NoteProperty").Name).attributes
# Output will be like this
tag_Chassis : Vm
tag_ServerFamily : WindowsServer
tag_criticality : prod
tag_Application : AllApp
alias : some alias
ipaddress : 172.21.x.x
tag_networking : lan
我尝试从 check_mk with http-api "action=get_all_hosts"
获取我所有的主机
响应是 json
格式,看起来像这样:
"{"result": {"some host name": {"attributes": {"tag_Chassis": "Vm", "tag_ServerFamily": "WindowsServer", "tag_criticality": "prod", "tag_Application": "AllApp", "alias": "some alias", "ipaddress": "172.21.x.x", "tag_networking": "lan"}, "hostname": "some host name", "path": "windows"}}"
现在我尝试格式化响应但没有成功。如何将结果格式化为具有所有属性的 table
?
您粘贴的 JSON 不正确。它不应该在开头或结尾有引号。最后还漏了一个 }
。您可以使用任何在线工具验证它 like this.
一旦你有了正确的 JSON 应该是:
{"result": {"some host name": {"attributes": {"tag_Chassis": "Vm", "tag_ServerFamily": "WindowsServer", "tag_criticality": "prod", "tag_Application": "AllApp", "alias": "some alias", "ipaddress": "172.21.x.x", "tag_networking": "lan"}, "hostname": "some host name", "path": "windows"}}}
您可以在从 JSON:
转换属性后访问它们# Convert and save to variable
$convertedJSON = @"
{"result": {"some host name": {"attributes": {"tag_Chassis": "Vm", "tag_ServerFamily": "WindowsServer", "tag_criticality": "prod", "tag_Application": "AllApp", "alias": "some alias", "ipaddress": "172.21.x.x", "tag_networking": "lan"}, "hostname": "some host name", "path": "windows"}}}
"@ | ConvertFrom-Json
# Access attributes
$convertedJSON.result.'some host name'.attributes
# If you don't know the hostname you can find it like this
($convertedJSON.result |Get-Member | ? MemberType -eq "NoteProperty").Name
# List all attributes from your JSON
$convertedJSON.result.$(($convertedJSON.result |Get-Member | ? MemberType -eq "NoteProperty").Name).attributes
# Output will be like this
tag_Chassis : Vm
tag_ServerFamily : WindowsServer
tag_criticality : prod
tag_Application : AllApp
alias : some alias
ipaddress : 172.21.x.x
tag_networking : lan