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 :
| 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