I'm testing code to send payment details as JSON which is working however I'm struggling to extract information from the JSON response. I need to extract the content part of the JSON response as individual values so I can assign them to variables then insert them into a SQL table.
The response is assigned to $Result . I've tried $Result.content though obviously this gives you the whole content section. Is there a way to obtain individual values from content without going down the substring route?
Here is an example of a response:
{"transaction":{"id":"83123b05-2435-40c9-851d-a5636f092637","processed_at":"2019-04-05T13:02:19.689188Z","transaction_type":"Debit","currency_code":"GBP","amount":25000,"recipient_id":"8a659242-8e70-47e1-85ca-2fe18cb262a0","status":"Waiting for funds","status_info":"Insufficient Funds To Process","reconciliation":null,"reference":"D000000516","account_id":"07b286ad-dabc-42a7-ab1b-5302fd382e6c","tag":null,"end_balance":null,"idempotent_key":"1f25e9f8-64cf-4e91-b4d3-8753437b6dbb","created_at":"2019-04-05T13:02:19Z","updated_at":"2019-04-05T13:02:19Z"}}
Here is my Powershell Code:
cls
$Uri = "uriaddress"
$Content = Get-content C:\Test\BACS-DC190326093019.TXT | select-object -skip 4
foreach($line in $Content){
if($line.Substring(0,1) -match "[0-9]")
{
$sorcode = $line.Substring(0,6)
$sorcode
$accountNumber = $line.Substring(6,8)
$accountNumber
$CustomerName = $line.Substring(82,18)
$CustomerName
$FundingAmount = $line.Substring(41,5)
$FundingAmount = $FundingAmount
$FundingAmount
$Identifier = $line.Substring(64,10)
$idempotent_key = New-Guid
$body = @{
"account_id" = "000000"
"currency_code" = "GBP"
"amount" = $FundingAmount
"recipient_name" = $CustomerName
"account_no" = $accountNumber
"sort_code" = $sorcode
"idempotent_key" = $idempotent_key
"reference" = $Identifier
"legal_type" = "PRIVATE"
}
$headers = @{}
$headers.Add("Authorization","00000000")
$Result = ''
$Result = Invoke-WebRequest -Uri $Uri -ContentType 'multipart/form-data' -Method Post -Headers $headers -Body $body
$Result.Content
IF ($Result.StatusDescription -eq 'OK') {
write-host "Payment Sent Succesfully" -ForegroundColor Green
} ELSE {
write-host "Payment Failed Succesfully" -ForegroundColor Red
}
write-host ""
}
}