簡體   English   中英

在SSIS中使用Powershell將JSON文件轉換為CSV

[英]Convert JSON file to CSV using powershell in SSIS

我們有一個JSON格式(腳本)的Web服務,需要將其轉換為csv文件並使用powershell放置到文件共享位置。 所有這些都需要在運行SSIS包時捕獲。 如何做到這一點?

# Set the filename to have the date suffix.
$fileName = "2758_RS-DM_AIRData" + (Get-Date -Format yyyyMMdd_hh) + ".csv"
$location = "C:\ExtractFilePowerShellWebService\Data"
New-Item -ItemType file -Name $fileName -Path $location 

# delete if file exist
$FileName2 = (Join-Path $location $fileName)
if (Test-Path $FileName2) {
  Remove-Item $FileName2

}

# Capture data from AIR Web Service

$webclient = new-object System.Net.WebClient
$webclient.Credentials = new-object System.Net.NetworkCredential("A", "B", "C")
$Json = $WebClient.Downloadstring("https://air.ciostage.accenture.com/ExternalServices/Application-service/Application(2758)")     
$jsonserial= New-Object -TypeName System.Web.Script.Serialization.JavaScriptSerializer 
$jsonserial.MaxJsonLength = [int]::MaxValue
$appCollection = @()
$appObj = $jsonserial.DeserializeObject($Json)
foreach($app in $appObj.value){

        $appObject = New-Object System.Object 
        $appObject | Add-Member -MemberType NoteProperty -Name "ApplicationId" -Value $app.ApplicationId 
        $appObject | Add-Member -MemberType NoteProperty -Name "ApplicationNm" -Value $app.ApplicationNm
        $appObject | Add-Member -MemberType NoteProperty -Name "Application Tier" -Value $app.ServiceLevelOfferingNm
        $appObject | Add-Member -MemberType NoteProperty -Name "Application Status" -Value $app.AppStatusNm
        #$appObject | Add-Member -MemberType NoteProperty -Name "Description" -Value $app.Description
        #$appObject | Add-Member -MemberType NoteProperty -Name "Acronym" -Value $app.Acronym
        #$appObject | Add-Member -MemberType NoteProperty -Name "Url" -Value $app.Url
        $appCollection += $appObject
}

#$appCollection | Export-Csv -Path "C:\ExtractFilePowerShellWebService\Data\2758_RS-DM_AIRData.csv" -notypeinformation

#extract data into CSV file.
$appCollection | Export-Csv  -Path (Join-Path $location $fileName) -notypeinformation 

當我運行腳本時,在提到的位置創建了csv文件,但它為空

我假設您的數據已經采用適合於csv的格式,因為Json有很多csv無法顯示的元素,所以在此示例中我使用ConvertTo-Json生成了Json文件:

Get-Process | ConvertTo-Json -Depth 1 | Out-File .\file.json

請注意-Depth 1 ,這意味着不會繼承任何嵌套數組等。

然后,我可以使用ConvertFrom-Json和以下內容將其轉換為csv:

(Get-Content .\file.json | ConvertFrom-Json) | Select Name,Handles,VM,PM | Export-Csv .\file.csv

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM