Trying to convert CSV into a particular JSON format - json

I need to convert a CSV to a particular JSON format but having trouble.
I currently have created the below powershell code which takes a CSV file with multiple columns and data for each column
enter code here $csvcontent = get-content "C:\tmp\vmfile.csv" | select -Skip 1
$Json =foreach($line in $csvcontent){
$obj = [PSCustomObject]#{
description = ($line -split ",")[0] -replace "`""
requestedFor = ($line -split ",")[1] -replace "`""
VMs = #{
vmType = $(($line -split ",")[5] -replace "`"");
environment = $(($line -split ",")[6] -replace "`"");
vmdescription = $(($line -split ",")[7] -replace "`"");
function = $(($line -split ",")[8] -replace "`"");
datacenter = $(($line -split ",")[9] -replace "`"");
Size = $(($line -split ",")[10] -replace "`"");
adDomain = $(($line -split ",")[11] -replace "`"");
Hostname = $(($line -split ",")[12] -replace "`"")
}
ExtraDisks = #{
VolumeName = $(($line -split ",")[14] -replace "`"");
VolumeLetter = $(($line -split ",")[15] -replace "`"");
Size = $(($line -split ",")[16] -replace "`"")
}
}
$obj | ConvertTo-Json
}
$json -replace '(?<=:\s+){','[ {' -replace '(?<="\s+)}','} ]'
This then generates the following json file which is not what i need as i want it all to come under the VM brackets not have a separate one for each VM
enter code here
{
"requestedFor": "John Doe",
"VMs": {
"Size": "Medium",
"datacenter": "DC1",
"environment": "dev",
"adDomain": "mydomain.com",
"vmType": "Windows Server",
"vmdescription": "VM Build1",
"function": "app",
"Hostname": "VMBuild1"
},
"ExtraDisks": {
"VolumeLetter": "G",
"Size": "10",
"VolumeName": "Logs"
}
}
{
"requestedFor": "John Doe",
"VMs": {
"Size": "Medium",
"datacenter": "DC2",
"environment": "prod",
"adDomain": "mydomain.com",
"vmType": "Windows Server",
"vmdescription": "VM Build2",
"function": "app",
"Hostname": "VMBuild2"
},
"ExtraDisks": {
"VolumeLetter": "E",
"Size": "50",
"VolumeName": "Data"
}
}
but what i need it to look like this
enter code here
{
"requestedFor": "John Doe",
"VMs": [ {
"vmType": "Windows Server",
"environment": "dev",
"description": "VMBuild1",
"function": "app",
"datacenter": "DC1",
"size": "Medium",
"adDomain": "mydomain.com",
"Hostname": "VMBuild1",
"ExtraDisks": [ {
"VolumeName": "Logs",
"VolumeLetter": "G",
"VolumeSize": 10
}
]
},
{
"vmType": "Windows Server",
"environment": "prod",
"description": "VMBuild2",
"function": "app",
"datacenter": "DC2",
"size": "Medium",
"adDomain": "mydomain.com",
"Hostname": "VMBuild2",
"ExtraDisks": [ {
"VolumeName": "Data",
"VolumeLetter": "E",
"VolumeSize": 50
}
]
}
]
}
Here is the CSV file contents
vmType environment description function datacenter Size adDomain Hostname VolumeName VolumeLetter VolumeSize
Windows Server dev VMBuild1 app DC1 Medium mydomain.com VMBUILD1 Logs G 10
Windows Server prod VMBuild2 app DC2 Medium mydomain.com VMBUILD2 Data E 50

Although your example CSV doesn't show it (copy/paste from Excel), I'm assuming it looks like this when opened in Notepad:
"vmType","environment","description","function","datacenter","Size","adDomain","Hostname","VolumeName","VolumeLetter","VolumeSize"
"Windows Server","dev","VMBuild1","app","DC1","Medium","mydomain.com","VMBUILD1","Logs","G","10"
"Windows Server","prod","VMBuild2","app","DC2","Medium","mydomain.com","VMBUILD2","Data","E","50"
"Windows Server","dev","VMBuild1","app","DC1","Medium","mydomain.com","VMBUILD1","Scripts","H","25"
The CSV does not have a column for RequestedFor, so the code below uses that as hardcoded variable.
Instead of reading the csv as string array and doing a lot of splitting and removing quote characters, you need to use Import-Csv.
After that, the only thing left to do is the way you want the final JSON formatted.
$requestor = 'John Doe'
$csvData = Import-Csv -Path 'D:\Test\vmfile.csv'
# get an array of PSObjects
# we use 'Group-Object Hostname' here to allow VMs with multiple extra disks
$allVMs = $csvData | Group-Object Hostname | ForEach-Object {
$disks = $_.Group | Select-Object VolumeName, VolumeLetter, VolumeSize
$vm = $_.Group[0] | Select-Object * -ExcludeProperty VolumeName, VolumeLetter, VolumeSize
$vm | Add-Member -MemberType NoteProperty -Name 'ExtraDisks' -Value #($disks)
# output the VM object
$vm
}
# combine the requestor, main element 'VMs' and the objects
# gathered above into a new object and convert that to JSON
[PsCustomObject]#{
RequestedFor = $requestor
VMs = #($allVMs)
} | ConvertTo-Json -Depth 4
Output:
{
"RequestedFor": "John Doe",
"VMs": [
{
"vmType": "Windows Server",
"environment": "dev",
"description": "VMBuild1",
"function": "app",
"datacenter": "DC1",
"Size": "Medium",
"adDomain": "mydomain.com",
"Hostname": "VMBUILD1",
"ExtraDisks": [
{
"VolumeName": "Logs",
"VolumeLetter": "G",
"VolumeSize": "10"
},
{
"VolumeName": "Scripts",
"VolumeLetter": "H",
"VolumeSize": "25"
}
]
},
{
"vmType": "Windows Server",
"environment": "prod",
"description": "VMBuild2",
"function": "app",
"datacenter": "DC2",
"Size": "Medium",
"adDomain": "mydomain.com",
"Hostname": "VMBUILD2",
"ExtraDisks": [
{
"VolumeName": "Data",
"VolumeLetter": "E",
"VolumeSize": "50"
}
]
}
]
}
Of course, you can save this in a json file, by appending | Set-Content -Path 'TheOutputFile.json' to it.
P.S. PowerShell does not produce 'pretty' json. If you need to convert it to properly spaced json, see my function Format-Json

You don't need to parse the csv yourself.
That's what ConvertFrom-Csv / Import-CSV are for.
Here's how I'd do it.
$CSVObj = get-content "C:\tmp\vmfile.csv" -Raw | ConvertFrom-Csv
$CSVObj | ConvertTo-Json | Set-Content "C:\tmp\vmfile.json"
That's all !
But let's go further. There was no CSV sample in your question so one might assume that the output JSON might still be incorrect. How would you make sure to have the format you want ?
By creating a brand new object structure from the imported object and then exporting it.
Here's a simple expression of what that might look like.
$CSVObj = get-content "C:\tmp\vmfile.csv" -Raw | ConvertFrom-Csv
# Create a new object from $csvObj that you will then export to csv
$Output = foreach ($item in $CSVObj) {
[PSCustomObject]#{
Requester = $item.requestedFor
VMs = $item.VMs
Count = $item.VMs.Count
}
}
$output | ConvertTo-Json | Set-Content "C:\tmp\vmfile.json"
You would then have successfully modified the json to be output to fit your needs.

Related

Powershell: JSON to Excel

I have had a problem for days and am now reporting here. I want to export several JSON files to an Excel spreadsheet. The JSON keys should form the headers and the values should be listed under the headers. Unfortunately, I have zero understanding of Powershell and can't get any further with the help of other threads, as I also don't understand why something works or doesn't work the way it does.
The json files look something like this
{"dataCollection": [
{
"objectID": 000001,
"randomID": 123,
"desc": "The sky is blue",
"startTime": "2022-03-15T11:31:56.510",
"endTime": "2022-03-15T11:31:56.511",
"caseOne": true,
"caseTwo": false,
"caseThree": null
},
{
"objectID": 333222,
"randomID": 456,
"desc": "example",
"startTime": "2022-03-15T11:31:56.510",
"endTime": "2022-03-15T11:31:56.511",
"caseOne": false,
"caseTwo": true,
"caseThree": null
},
{
"objectID": 111111,
"randomID": 789,
"desc": "Mo-Fr 60% 20-24",
"startTime": "2022-03-15T11:31:56.510",
"endTime": "2022-03-15T11:31:56.511",
"caseOne": false,
"caseTwo": false,
"caseThree": null
}
]}
My current code looks like this
$contentJson = Get-Content -Raw -Path $jsonInput | ConvertFrom-Json
$obj_list = $contentJson | Select-Object #{Name='Name';Expression={$_}}
$obj_list | Export-Csv $csvOutput -NoType -Delimiter "`t" -Encoding Unicode
(Get-Content -Path $csvOutput -Raw).replace('"','') | Set-Content -Path $csvOutput
This does give me a CSV with the information from the json, however it is transferred cell by cell and I have no idea how to create headers. Further this works at all only, as soon as I remove in the first line of the JSON (in this case {"DataCollection":), otherwise in the Excel table only the following is written: #{ttDebugTage=System.Object[]}
My goal is something looking like this:
Excel:
This is the first time I'm working with Powershell and unfortunately I'm completely lacking in understanding, so I would appreciate any help.
$contentJson = #'
{"dataCollection": [
{
"objectID": 000001,
"randomID": 123,
"desc": "The sky is blue",
"startTime": "2022-03-15T11:31:56.510",
"endTime": "2022-03-15T11:31:56.511",
"caseOne": true,
"caseTwo": false,
"caseThree": null
},
{
"objectID": 333222,
"randomID": 456,
"desc": "example",
"startTime": "2022-03-15T11:31:56.510",
"endTime": "2022-03-15T11:31:56.511",
"caseOne": false,
"caseTwo": true,
"caseThree": null
},
{
"objectID": 111111,
"randomID": 789,
"desc": "Mo-Fr 60% 20-24",
"startTime": "2022-03-15T11:31:56.510",
"endTime": "2022-03-15T11:31:56.511",
"caseOne": false,
"caseTwo": false,
"caseThree": null
}
]}
'#
($contentJson | ConvertFrom-Json).dataCollection |
Select-Object -Property objectID, randomID, desc |ConvertTo-Csv -Delimiter "`t"
"objectID" "randomID" "desc"
"1" "123" "The sky is blue"
"333222" "456" "example"
"111111" "789" "Mo-Fr 60% 20-24"
################## PFADE ###################
$jsonDirectory = 'DIR\TO\JSON\FILES'
$csvFile = 'DIR\TO\OUTPUT\FILE.CSV'
################ Variablen #################
$excel = New-Object -ComObject Excel.Application
############################################
#ALLE JSON FILES
$jsonFiles = Get-ChildItem -Path $jsonDirectory -Filter *.json
#FILTER
$unwantedKeys = #("Example1", "Example2", "Example3")
#ARRAY ZWISCHENSPEICHER
$jsonData = #()
foreach ($jsonFile in $jsonFiles) {
#Lädt JSON File
$json = Get-Content $jsonFile.FullName | ConvertFrom-Json
$firstKey = ($json.PSObject.Properties.Name)
#Filter
$json = $json.$firstKey | Select-Object * -ExcludeProperty $unwantedKeys
#Content ins Array
$jsonData += $json
}
#Erstellt CSV und Importiert JSON Content
$jsonData | Export-Csv $csvFile -NoTypeInformation -Delimiter "`t" -Encoding Unicode
#Anpassen von Spaltenbreite auf Valuelänge
$Workbook = $excel.Workbooks.Open($csvFile)
$Worksheet = $Workbook.Sheets.Item(1)
$range = $worksheet.UsedRange
$range.EntireColumn.AutoFit()
$excel.Visible = $True

powershell that reads values from csv or json file and create registry keys if do not exist

I am trying to write a powershell script that creates registry keys and their values from a csv or json file containing the list of the registries .
did any one code such thing can help ? :)
Using ConvertFrom-Json, convert your JSON to a custom PSCustomObject object. Then iterate through the object properties and call New-ItemProperty to create the new registry entries with the appropriate values.
PowerShell
$jsonFile = "<path to JSON file>"
$customObject = Get-Content $jsonFile | ConvertFrom-Json
$customObject.PSObject.Properties | ForEach-Object {
[void](New-ItemProperty -LiteralPath $_.Value.path -Name $_.Value.name -Value $_.Value.value -PropertyType $_.Value.type -Force)
}
JSON
{
"reg1": {
"path": "path1",
"name": "name1",
"value": "value1",
"type": "type1"
},
"reg2": {
"path": "path2",
"name": "name2",
"value": "value2",
"type": "type2"
},
"reg3": {
"path": "path3",
"name": "name3",
"value": "value3",
"type": "type3"
}
}
Links
ConvertFrom-Json (learn.microsoft.com)
New-ItemProperty (learn.microsoft.com)

Seperating Json objects based on keyvalue

currently i am working on fetching Azure ad application expiry status on that i have pulled around 1700 ad applications and i have added one key pair(status) to the json object based on the secret expiry date
1) valid 2) Expired 3) expiring soon
so i have extracted all applications to a json file now i need to split single file into 3 files based on status as mentioned below
[
{
"DisplayName": "Reporter-dev",
"ObjectId": null,
"ApplicationId": {
"value": "62838283828288282828828288282828",
"Guid": "62838283828288282828828288282828"
},
"KeyId": "62838283828288282828828288282828",
"Type": "Password",
"StartDate": {
"value": "/Date(1590537256000)/",
"DateTime": "27 May 2020 05:24:16"
},
"EndDate": {
"value": "/Date(1653609256000)/",
"DateTime": "27 May 2022 05:24:16"
},
"Ownername": "shetty#gmail.com",
"Status": "Valid"
},
{
"DisplayName": "azure-cli-2018",
"ObjectId": null,
"ApplicationId": {
"value": "52388282828828288273673282932739223",
"Guid": "52388282828828288273673282932739223"
},
"KeyId": "52388282828828288273673282932739223",
"Type": "Password",
"StartDate": {
"value": "/Date(1568849784000)/",
"DateTime": "19 September 2019 05:06:24"
},
"EndDate": {
"value": "/Date(1600472184000)/",
"DateTime": "19 September 2020 05:06:24"
},
"Ownername": "joseph#gmail.com",
"Status": "Expired"
},
{
"DisplayName": "azure-cli-2019",
"ObjectId": null,
"ApplicationId": {
"value": "26382882828828282882828282828",
"Guid": "26382882828828282882828282828"
},
"KeyId": "26382882828828282882828282828",
"Type": "Password",
"StartDate": {
"value": "/Date(1576143476000)/",
"DateTime": "12 December 2019 15:07:56"
},
"EndDate": {
"value": "/Date(1607765876000)/",
"DateTime": "12 December 2020 15:07:56"
},
"Ownername": "zzzzzzzzz#gmail.com",
"Status": "About to Expire"
}
]
The below will split out the JSON based on the status and convert the data back to JSON. Change $JSONPath, $ValidPath, $ExpiredPath, $ExpiredSoonPath to the paths you require, the ones currently populated are what I have used for testing.
The contents of $JSONPath must have valid JSON to be able to work, whilst this is probably not the most efficient nor elegant it should do what you need.
$JSONPath = "C:\PS\JT\JSON.txt"
$JSONObj = Get-Content $JSONPath | ConvertFrom-Json
$ValidPath = "C:\PS\JT\Valid.txt"
$ExpiredPath = "C:\PS\JT\Expired.txt"
$ExpireSoonPath = "C:\PS\JT\ExpireSoon.txt"
$JSONObj | Where {$_.Status -eq "Valid"} | ConvertTo-Json | Out-File $ValidPath
$JSONObj | Where {$_.Status -eq "Expired"} | ConvertTo-Json | Out-File $ExpiredPath
$JSONObj | Where {$_.Status -eq "About to Expire"} | ConvertTo-Json | Out-File $ExpireSoonPath
Something very simple like the below example should work in theory but since you won't show your code there's a bunch of guess work here...
$collection = $json | ConvertFrom-Json
$valid = #(); $exp = #(); $soon = #(); $unknown = #()
foreach($item in $collection) {
switch ($item.Status)
{
'Valid' { $valid += $item }
'Expired' { $exp += $item }
'About to Expire' { $soon += $item }
Default { $unknown += $item}
}
}
$valid | Out-File .\Valid.txt -Append
$exp | Out-File .\Expired.txt -Append
$soon | Out-File .\AboutToExpire.txt -Append
$unknown | Out-File .\Unknown.txt -Append
Or if json is the desired output as in the other example;
$valid | ConvertTo-Json | Out-File .\Valid.txt -Append
etc, etc
Adding to the other answers, here is another solutions using pipelines.
Get-Content -Path .\sample.json -Raw | ConvertFrom-Json |
ForEach-Object { $_ | ConvertTo-Json | Out-File -FilePath "$($_.Status).json" }
Explanation
Parse JSON content using Get-Content, also making sure we use the -Raw switch to return the contents as one string with the newlines preserved. We don't need the contents as an array of strings(default behaviour).
Pipe to ConvertFrom-Json to deserialize the JSON into a System.Management.Automation.PSCustomObject.
Iterate over the objects using Foreach-Object and convert the object to a JSON file using ConvertTo-Json and Out-File. Note the default depth for ConvertTo-Json is 2, so if your JSON files end up having deeper levels, then you will need to specify a larger depth using the -Depth switch.
If its easier understand, you can also use a regular foreach loop here:
$json = Get-Content -Path .\sample.json -Raw | ConvertFrom-Json
foreach ($object in $json) {
$object | ConvertTo-Json | Out-File -FilePath "$($object.Status).json"
}

Convert Azure DevOps rest api test plans for all projects to csv file using powershell

I am trying to generate report for all the test plans available in azure DevOps organization. here is my script which generates list of projects and the iterate through every project to find test plans available in it. I want all this data to be saved in csv file. I am able to get json file. Is there any way I can get this data saved in csv file with every project iterate for test plan ?
$connectionToken = ""
$BaseUrl = "https://dev.azure.com/{organization_name}/_apis/projects?
api-versions=5.0"
$base64AuthInfo=
[System.Convert]::ToBase64String([System.Text.Encoding]
'::ASCII.GetBytes(":$($connectionToken)"))
$ProjectInfo = Invoke-RestMethod -Uri $BaseUrl -Headers
#{authorization = "Basic $base64AuthInfo"} -
Method Get
$ProjectDetails = $ProjectInfo | ConvertTo-Json -Depth 100
$Projectname = $ProjectInfo.value.name
ForEach ($project in $Projectname){
$TestPlanApi = "https://dev.azure.com/{org}/$project/_apis/test/plans?
api-version=5.0"
$TestplanInfo = Invoke-RestMethod -Uri $TestPlanApi -Headers
#{authorization = "Basic $base64AuthInfo"} -Method Get
if (-NOT ($TestplanInfo.count -eq 0)){
$info = $TestplanInfo | ConvertTo-Json -Depth 100
$info
}
}
This gives me following json file , I want to convert in to csv. Due to every project test plans value starts the Json result with value I am not able to expand it and save to csv
{
"value": [
{
"id": 134,
"name": "sprint1",
"url": "https://dev.azure.com/fabrikam/fabrikam-fiber-
tfvc/_apis/test/Plans/1",
"project": {
"id": "eb6e4656-77fc-42a1-9181-4c6d8e9da5d1",
"name": "Fabrikam-Fiber-TFVC",
"url":
"https://dev.azure.com/fabrikam/_apis/projects/Fabrikam-
Fiber-
TFVC"
},
"area": {
"id": "343",
"name": "Fabrikam-Fiber-TFVC"
},
"iteration": "Fabrikam-Fiber-TFVC\\Release 1\\Sprint 1",
"state": "Active",
"rootSuite": {
"id": "1"
},
"clientUrl": "mtms://fabrikam.visualstudio.com:443/DefaultCollection/p:Fabrikam-
Fiber-TFVC/Testing/testplan/connect?id=1"
}
],
"count": 1
}
{
"value": [
{
"id": 567,
"name": "sprint1",
"url": "https://dev.azure.com/fabrikam/fabrikam-fiber-
tfvc/_apis/test/Plans/1",
"project": {
"id": "eb6e4656-77fc-42a1-9181-4c6d8e9da5d1",
"name": "Fabrikam-Fiber-TFVC",
"url": "https://dev.azure.com/fabrikam/_apis/projects/Fabrikam-Fiber-
TFVC"
},
"area": {
"id": "343",
"name": "Fabrikam-Fiber-TFVC"
},
"iteration": "Fabrikam-Fiber-TFVC\\Release 1\\Sprint 1",
"state": "Active",
"rootSuite": {
"id": "1"
},
"clientUrl":"mtms://fabrikam.visualstudio.com:443/DefaultCollection/p:Fabrikam-
Fiber-TFVC/Testing/testplan/connect?id=1"
},
{
"id": 678,
"name": "sprint1",
"url": "https://dev.azure.com/fabrikam/fabrikam-fiber-
tfvc/_apis/test/Plans/1",
"project": {
"id": "eb6e4656-77fc-42a1-9181-4c6d8e9da5d1",
"name": "Fabrikam-Fiber-TFVC",
"url": "https://dev.azure.com/fabrikam/_apis/projects/Fabrikam-Fiber-
TFVC"
},
"area": {
"id": "343",
"name": "Fabrikam-Fiber-TFVC"
},
"iteration": "Fabrikam-Fiber-TFVC\\Release 1\\Sprint 1",
"state": "Active",
"rootSuite": {
"id": "1"
},
"clientUrl":
"mtms://fabrikam.visualstudio.com:443/DefaultCollection/p:Fabrikam-
Fiber-TFVC/Testing/testplan/connect?id=1"
}
],
"count": 2
}
These are the values for different projects test runs, some projects have count = 1 then it shows one id , some projects has count =3 then it shows all the 3 ids and so on
I want this json file in csv file with columns -
id , name , url , project.name , project.id , project.url ,area.id , area.name , iteration , owner , revision , state , rootsuite.id , clienturl
How can I expand all the values in csv file ? I tried
Select-object Expand-property value but its fails to to expand all the values in json data
As a workaround, we can save the response body and then convert the json file to csv file.
We cannot save all info in one csv file, the latest info will overwrite the old data, so we need to save the test plan info in different csv files.
Sample:
$connectionToken = "{PAT}"
$BaseUrl = "https://dev.azure.com/{Org}/_apis/projects?api-version=6.1-preview.4"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$ProjectInfo = Invoke-RestMethod -Uri $BaseUrl -Headers #{authorization = "Basic $base64AuthInfo"} -Method Get
$ProjectDetails = $ProjectInfo | ConvertTo-Json -Depth 100
$Projectname = $ProjectInfo.value.name
ForEach ($project in $Projectname){
$TestPlanApi = "https://dev.azure.com/{Org}/$project/_apis/test/plans?api-version=5.0"
$TestplanInfo = Invoke-RestMethod -Uri $TestPlanApi -Headers #{authorization = "Basic $base64AuthInfo"} -Method Get
if (-NOT ($TestplanInfo.count -eq 0)){
#Save the test plan info to json file
$info = $TestplanInfo | ConvertTo-Json -Depth 100 | out-file E:\test\$project.json
#convert the json file to csv file
Get-Content -Path E:\test\$project.json | ConvertFrom-Json | Select-Object -expand value | ConvertTo-Csv -NoTypeInformation | Set-Content E:\test\$project.csv
#delete json file
Remove-Item E:\test\$project.json
}
}
Result:

Powershell - json to text or csv

I have a folder with hundreds of json files in it & need to read them & create an output file with the various fields & values in it.
{
"id": "02002010",
"booktitle": "",
"pagetitle": "Demo Page",
"parent": "02002000",
"img": [
{
"imgfile": "02A.png",
"imgname": "02A.png"
}
],
"fmt": "",
"entries": [
{
"itemid": "1",
"partnumber": "1234567",
"partdescription": "Washer",
"partqty": "2",
"Manufacturer": "ACME",
"partdescriptionlocal": "Washer"
},
{
"itemid": "2",
"partnumber": "98765-B",
"partdescription": "Screw",
"partqty": "8",
"Vendor": "Widget Inc",
"TYPE": "Galv",
"partdescriptionlocal": "Screw"
}]
}
The json files will have generally the same structure, except that the "entries" may contain various fields in it that may not be the same from one entry to the next, or one json file to the next. Some may have fields within entry that I do not know the name of. There will be a few common fields in each "entries" section, but they could vary, and could be in a different order than what is shown.
I would like to write the output to a text/csv file that would be delimited that could then be imported into Excel. One column header with all fields listed. As new "entries" fields are found, tack them on to the end of each row & add to the header also.
you mean to do something like this?
$json = gc C:\temp\file.json | ConvertFrom-Json
$props = $json.entries | % {$_ | gm -MemberType NoteProperty} | select -exp name -Unique
$results = #()
foreach ($entry in $json.entries) {
$obj = $json | select *
foreach ($prop in $props) {
$obj | Add-Member -MemberType NoteProperty -Name $prop -Value $($entry | select -exp $prop -ea 0)
}
$results += $obj
}
$results | epcsv C:\temp\file.csv -NoTypeInformation -Encoding ASCII
$results