简体   繁体   中英

Powershell ConvertFrom-Json Loop Through Multiple Files to Output to CSV

I have a folder with dozens of json files that I output to csv via powershell.All the json files have the same format. My current powershell script is defining each file one by one by the file name, selecting various values and outputting it to the same csv.

How do I modify the powershell script so that it simply goes through each file in the folder without having to define each one by its file name specifically?

Each json's content is like this:

   {
      "ItemName": "A",
      "ItemID": "I001",
      "ItemDate": "2021-03-01",
      "ItemValue": "1000",
      "ItemTags": [
         {
            "Name": "Tag 1",
            "Value": "medium"
         },
         {
            "Name": "Tag 2",
            "Value": "red"
         },
         {
            "Name": "Tag 3",
            "Value": null
         },
         {
            "Name": "Tag 4",
            "Value": "Yes"
         }
      ]
   },
   {
      "ItemName": "B",
      "ItemID": "I002",
      "ItemDate": "2021-02-01",
      "ItemValue": "3000",
      "ItemTags": [
         {
            "Name": "Tag 1",
            "Value": "best"
         },
         {
            "Name": "Tag 2",
            "Value": "green"
         },
         {
            "Name": "Tag 3",
            "Value": null
         },
         {
            "Name": "Tag 4",
            "Value": "No"
         }
      ]

Here's a sample of the powershell. How do I modify it so goes through the C:\temp folder and do the same output for each file without the -Path part needing to be specified with the file name?

$obj1 = Get-Content -Path "C:\Temp\sample1.json"  | ConvertFrom-Json
$obj1 | select ItemName, ItemID, ItemDate, ItemValue, @{Name = 'Tag 4'; Expression ={($_.itemtags | where-object Name -eq "Tag 4").Value}} | Export-CSV "C:\Temp\items.csv" -NoTypeInformation -Append

$obj2 = Get-Content -Path "C:\Temp\sample2.json"  | ConvertFrom-Json
$obj2 | select ItemName, ItemID, ItemDate, ItemValue, @{Name = 'Tag 4'; Expression ={($_.itemtags | where-object Name -eq "Tag 4").Value}} | Export-CSV "C:\Temp\items.csv" -NoTypeInformation -Append

$obj3 = Get-Content -Path "C:\Temp\sample3.json"  | ConvertFrom-Json
$obj3 | select ItemName, ItemID, ItemDate, ItemValue, @{Name = 'Tag 4'; Expression ={($_.itemtags | where-object Name -eq "Tag 4").Value}} | Export-CSV "C:\Temp\items.csv" -NoTypeInformation -Append

Try with this:

$properties = @(
    'ItemName', 'ItemID', 'ItemDate'
    'ItemValue', @{
        Name = 'Tag 4'
        Expression = {
            ($_.itemtags | Where-Object Name -eq "Tag 4").Value
        }
    }
)

Get-ChildItem "C:\Temp" -File -Filter *.json | ForEach-Object {
    Get-Content $_ -Raw | ConvertFrom-Json |
    Select-Object $properties
} | Export-CSV "C:\Temp\items.csv" -NoTypeInformation

Edit

I reproduced the JSON from your example, if all JSONs look like this let me know and I'll edit my code.

PS \> $json[0]

ItemName  : A
ItemID    : I001
ItemDate  : 2021-03-01
ItemValue : 1000
ItemTags  : {@{Name=Tag 1; Value=medium}, @{Name=Tag 2; Value=red}, @{Name=Tag 3; Value=}, @{Name=Tag 4; Value=Yes}}

ItemName  : B
ItemID    : I002
ItemDate  : 2021-02-01
ItemValue : 3000
ItemTags  : {@{Name=Tag 1; Value=best}, @{Name=Tag 2; Value=green}, @{Name=Tag 3; Value=}, @{Name=Tag 4; Value=No}}

PS \> $json[0][0]

ItemName  : A
ItemID    : I001
ItemDate  : 2021-03-01
ItemValue : 1000
ItemTags  : {@{Name=Tag 1; Value=medium}, @{Name=Tag 2; Value=red}, @{Name=Tag 3; Value=}, @{Name=Tag 4; Value=Yes}}

Edit 2

$jsonFiles = Get-ChildItem "C:\temp" -File -Filter *.json

$result = foreach($json in $jsonFiles)
{
    $content = Get-Content $json -Raw | ConvertFrom-Json
    
    foreach($element in $content)
    {
        $tag4 = $element.itemtags.where({$_.Name -eq "Tag 4"}).Value

        [pscustomobject]@{
            ItemName = $element.ItemName
            ItemID = $element.ItemID
            ItemDate = $element.ItemDate
            ItemValue = $element.ItemValue
            Tag4 = $tag4
        }
    }
}

$result | Export-Csv "C:\temp\items.csv" -NoTypeInformation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM