Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Build an array with powershell from json file

I have a json file that I would like to export or convert in csv or table format

My json source file is like this :

[
   {
      "host_display_name" : "DCWKS10",
      "host_groups" : [
         "Windows",
         "WINDOWS"
      ],
      "next_check" : 1698741835,
      "notes" : "",
      "notes_expanded" : "",
      "notes_url" : "",
      "notes_url_expanded" : "",
      "notification_interval" : 0.0,
      "notification_period" : "TP",
      "notifications_enabled" : 1,
      "obsess_over_service" : 1,
      "peer_key" : "7215e",
      "peer_name" : "Nagios",
      "percent_state_change" : 0.0,
      "perf_data" : "'E:\\ Used Space'=1475.08Gb;1474.56;1556.48;0.00;1638.40",
      "plugin_output" : "E:\\ - total: 1638.40 Gb - used: 1475.08 Gb (90%) - free 163.32 Gb (10%)",
      "process_performance_data" : 1,
      "retry_interval" : 1.0,
      "scheduled_downtime_depth" : 0,
      "state" : 1,
      "state_order" : 1,
      "state_type" : 1
   },
   {
      "host_display_name" : "DCWKS50",
      "host_groups" : [
         "Windows",
         "WINDOWS"
      ],
      "next_check" : 1698741896,
      "notes" : "",
      "notes_expanded" : "",
      "notes_url" : "",
      "notes_url_expanded" : "",
      "notification_interval" : 0.0,
      "notification_period" : "TP",
      "notifications_enabled" : 1,
      "obsess_over_service" : 1,
      "peer_key" : "7215e",
      "peer_name" : "Nagios",
      "percent_state_change" : 0.0,
      "perf_data" : "C=86.15;106.73;106.73;0;106.73 ",
      "plugin_output" : "CRITICAL - Disk D: 24.75 Gb (1.7%) Free",
      "process_performance_data" : 1,
      "retry_interval" : 1.0,
      "scheduled_downtime_depth" : 0,
      "state" : 2,
      "state_order" : 4,
      "state_type" : 1
   }
]

I have all variables $jsonObj.host_display_name, $jsonObj.host_groups …. but I don’t know how to build an array formatted like this :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

host_display_name host_groups
DCWKS10 WINDOWS
DCWKS50 WINDOWS

Thank you for your help

>Solution :

Here you can make use of PowerShell’s ConvertFrom-Json commandlet and Format-Table:

# Import the file contents and convert from Json to a PowerShell object.
$JsonData = Get-Content "source.json" | ConvertFrom-Json

# Select the headings you want, and format as a table
$JsonData | Select-Object host_display_name,host_groups | Format-Table
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading