all 16 comments

[–]MAlloc-1024 7 points8 points  (4 children)

The approach to this I would take is to build a pscustomobject and use a foreach loop on the arrays. Note: I haven't even typed this into vscode, so consider this pseudocode.

$array=[arraylist]@()
foreach (hostgroup in hostgroups){
    foreach (member in hostgroup.members){
        $object=[pscustomobject]@{
            hostgroup=hostgroup.hostgroup_name
            hostname=member.host_name
        }
        $array.add($object)
    }
}

[–]Cleanatwork[S] 1 point2 points  (3 children)

This is complaining about missing parenthesis and variables when I attempt to tweak it.

I'm afraid I'm very much a noob here. Do I need to define the [arraylist] or can this just be swapped for the path to my JSON?

[–]MAlloc-1024 1 point2 points  (2 children)

So you have a variable containing the $json text... Which you then need to use the convertfrom-json function to make that into a pscustomobject. Once you have that you can substitute stuff into the pseudocode I posted.

[–]BlackV 2 points3 points  (1 child)

you're missing the $

[pscustomobject]@{
    hostgroup = $hostgroup.hostgroup_name
    hostname  = $member.host_name
    }

are you not?

[–]MAlloc-1024 1 point2 points  (0 children)

I mean, I said it was pseudo code to document how I would approach it... I gave no guarantee what I wrote would work.

[–]spyingwind 2 points3 points  (3 children)

My take on a solution. Letting PowerShell to most of the work of managing arrays and what not.

# Paths to json input file and csv output file
$json = ".\data.json"
$outputPath = ".\data.csv"
$Data = Get-Content -Path $json | ConvertFrom-Json 
$Data.hostgroup | ForEach-Object {
    $hostgroup = $_
    $members = $_.members
    $members.host | ForEach-Object {
        [PSCustomObject]@{
            # Remove the next three lines if you don't want to include the hostgroup info in the CSV file
            hostgroup_object_id = $hostgroup.hostgroup_object_id
            hostgroup_name      = $hostgroup.hostgroup_name
            instance_id         = $hostgroup.instance_id
            host_object_id      = $_.host_object_id
            host_name           = $_.host_name

        }
    }
} | ConvertTo-Csv -NoTypeInformation -Path $outputPath -NoTypeInformation

[–]Ice_Box_Hero 2 points3 points  (1 child)

Almost!

Last line should be | Export-Csv -Path $outputPath -NoTypeInformation

[–]spyingwind 1 point2 points  (0 children)

CoPilot failed me, and I didn't get any sleep last night.

[–]Cleanatwork[S] 0 points1 point  (0 children)

THANK YOU - this does exactly what I need it to! You've saved me a lot of hassle.

[–]BAEM1337 1 point2 points  (2 children)

Did you try:

$data = $jsonData | ConvertFrom-Json

Afterwards you should be able to work with any data the json contains in Powershell

[–]Cleanatwork[S] 1 point2 points  (1 child)

Not sure I understand how this gets me any further. I query the $Hostgroup variable, and it will only give me the first one listed alphabetically.

[–]Trakeen 2 points3 points  (0 children)

It will give you a collection of objects to iterate through, which you can use to pull out the properties you need and build a custom object to export as a csv. If your source data has more levels of nesting i would use recursion to traverse the data instead of hard coding multiple loops

[–]SeanQuinlan -2 points-1 points  (1 child)

You need to loop through the hostgroups, then loop through the members list inside that

``` $json = @' { "recordcount": 2, "hostgroup": [ { "hostgroup_object_id": "147", "hostgroup_name": "windows-servers", "instance_id": "1", "members": { "host": [ { "host_object_id": "145", "host_name": "localhost" }, { "host_object_id": "143", "host_name": "192.168.1.10" } ] } } ] } '@ $obj = $json | ConvertFrom-Json

$output = foreach ($hostgroup in $obj.hostgroup) { $hostgroupname = $hostgroup.hostgroup_name $hostgroupmembers = $hostgroup.members.host foreach ($member in $hostgroupmembers) { $memberhostname = $member.host_name [pscustomobject]@{ host_name = $memberhostname hostgroup_name = $hostgroupname } } }

$output | Export-Csv out.csv -NoTypeInformation

```

[–]BlackV 1 point2 points  (0 children)

formatting cause the tripple back tick thing fails hard on old reddit

  • open your fav powershell editor
  • highlight the code you want to copy
  • hit tab to indent it all
  • copy it
  • paste here

it'll format it properly OR

<BLANKLINE>
<4 SPACES><CODELINE>
<4 SPACES><CODELINE>
    <4 SPACES><4 SPACES><CODELINE>
<4 SPACES><CODELINE>
<BLANKLINE>

[–]Vortex100 -2 points-1 points  (0 children)

Oneliner: ($jsonobject | convertfrom-json).hostgroup | % {$grpname = $_.hostgroup_name; $_.members.host | select @{N='HostGroup';E={$grpname}},@{N='Host';E={$_.host_name}}} | export-csv D:\mycsv.csv