Thursday, November 9, 2017

Convert JSON file to CSV With Powershell

I'm trying to learn Powershell.  I tend to do better when I have real world puzzles to solve.

Today, someone wanted an inventory of devices from our firewall manager.

Luckily, you could export a list of the devices that it was managing, but it exported it in a .dat file.

That .dat file was compressed.  Fortunately, 7Zip seemed to know what type of compression it was, so I simply extracted the list using 7Zip.

The list was kind of json formatted.  I wanted a csv file because it's easier to work with.

I say kind of because if I opened it in Notepad++, it had extra data at the top and bottom.  Reason being is so that if I wanted to import the list back into the manager, the manager could process it.

I tried using the data as it was in Powershell.  Powershell did not like it one bit.

Get-Content -Raw list | ConvertFrom-Json

Invalid Json Primitive
<a bunch of red error messages here>

So, I opened it in Notepad++ and removed the extra data at the top and bottom of json data.  Still a bunch of red error messages.

Added a curly bracket at the top { and a curly bracket at the bottom}.  Powershell finally did something with it.  So, I tried this.

Get-Content -Raw list | ConvertFrom-Json | ConvertTo-Csv | Out-File list.csv

It kind of worked.  The data I wanted was in a couple of arrays though.  So I had to do this:

Get-Content -Raw list | ConvertFrom-Json | Select -Expand <array name here> | Select -Expand <another array name here> |  Select name,"serial number" | ConvertTo-Csv | Out-File list.csv

Bingo!  It had a header and a footer in the csv - something to do with Powershell, but it worked.  I just opened it in Excel and removed the header and footer.  I saved it as an Excel doc.  Seems to be fine.