0

I try to grab all my host from check_mk with http-api "action=get_all_hosts" the response is json format and look like this:

"{"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"}}" 

Now I try to format the response without success. How can I format the result to table with all the properties?

Robert Dyjas
  • 4,979
  • 3
  • 19
  • 34

1 Answers1

2

That JSON you pasted is incorrect. It shouldn't have quotes at the beginning nor at the end. And you're missing one } at the end. You can validate it using any online tool like this.

Once you have the correct JSON which should be:

{"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"}}}

You can access the attributes once you convert them from 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
Robert Dyjas
  • 4,979
  • 3
  • 19
  • 34
  • ($convertedJSON.result |Get-Member | ? MemberType -eq "NoteProperty").Name work good but $convertedJSON.result.$(($o.result |Get-Member | ? MemberType -eq "NoteProperty").Name).attributes return empty response – Oded Refaeli Jun 11 '19 at 09:55
  • @OdedRefaeli please check now, I messed up variable names, now it's corrected. – Robert Dyjas Jun 11 '19 at 10:25
  • @OdedRefaeli can you just copy my code (except last part) and let me know what you receive? Not sure if I follow what you mean and what you checked as you're not very precise with last comment. – Robert Dyjas Jun 11 '19 at 11:37
  • Your code output is as you write but when I run the same code against my JSON the result is empty so I guess the problem is in my JSON output. The JSON output passed the online validation tool. anyway thank you very much for your support – Oded Refaeli Jun 11 '19 at 13:40
  • Check how your JSON differs from the one I use (maybe different elements' names, maybe there are two elements somewhere while the example has only one) and you should be able to figure it out. In case you're stuck, post a question and remember to read best practices first: [ask]. Good luck! – Robert Dyjas Jun 11 '19 at 13:50
  • My JSON have more then 1000 element. I only cut the first one as example. – Oded Refaeli Jun 11 '19 at 14:00