0

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. All this needs to be captured on running a SSIS package. 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

18
  • Why does it need to be in a SSIS package? Just call the powershell script directly Commented May 30, 2017 at 8:40
  • @Nick.McDermaid because an SSIS package does a lot more than just convert one format to another. It will load, transform, lookup, combine and bulk load data from a LOT of different sources and send it to a LOT of different targets. AND validate fields, sizes etc to ensure you won't truncate data by accident, or try to pass fewer columns than those expected, or send a number to a string field. Trying to do the same with a Powershell script can be .... fun Commented May 30, 2017 at 8:42
  • @Aiswarya what does the JSON file look like? Converting/loading flat objects should be easy. Converting nested objects would require transformations eg with Select to flatten them or extract only the necessary fields Commented May 30, 2017 at 8:45
  • @Aiswarya furthermore, SQL Server 2016 introduced JSON support and querying. Maybe you could load individual Json rows into a table, then query them Commented May 30, 2017 at 8:46
  • Just establishing whether SSIS is required or not. If all they are doing is converting JSON to CSV then it's probably overkill. Commented May 30, 2017 at 8:46

1 Answer 1

1

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 :

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

note the -Depth 1 which means no nested arrays etc. will be carried over.

Then I can convert that to to a csv using ConvertFrom-Json and the following:

(Get-Content .\file.json | ConvertFrom-Json) | Select Name,Handles,VM,PM | Export-Csv .\file.csv
Sign up to request clarification or add additional context in comments.

3 Comments

In other words, the OP should use Convert-From
@PanagiotisKanavos i don't think gc .. | convertfrom-json | export-csv works though, covnertfrom-json is weird when piped directly.
The answer will be a lot more useful if you explain that you can convert to/from Json already, which functions to use and add links to them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.