简体   繁体   English

在SSIS中使用Powershell将JSON文件转换为CSV

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

We have a Web Service which is in JSON format(script) that needs to be converted to csv files and dropped to a fileshare location using powershell. 我们有一个JSON格式(脚本)的Web服务,需要将其转换为csv文件并使用powershell放置到文件共享位置。 All this needs to be captured on running a SSIS package. 所有这些都需要在运行SSIS包时捕获。 How can this be achieved? 如何做到这一点?

# 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 

When I run the script, csv file is created in the location mentioned but it is empty 当我运行脚本时,在提到的位置创建了csv文件,但它为空

I'm assuming your data is already in a format that will suit being in a csv, as Json has a lot of elements which csv cannot display, i'm using ConvertTo-Json in this example to generate a Json file : 我假设您的数据已经采用适合于csv的格式,因为Json有很多csv无法显示的元素,所以在此示例中我使用ConvertTo-Json生成了Json文件:

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

note the -Depth 1 which means no nested arrays etc. will be carried over. 请注意-Depth 1 ,这意味着不会继承任何嵌套数组等。

Then I can convert that to to a csv using ConvertFrom-Json and the following: 然后,我可以使用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