Powershell:从JSON响应中选择单个值

时间:2019-04-05 13:20:09

标签: sql json powershell

我正在测试将付款明细作为JSON发送的代码,但是我正在努力从JSON响应中提取信息。 我需要将JSON响应的内容部分提取为单个值,以便可以将其分配给变量,然后将其插入到SQL表中。

响应分配给$ Result。我尝试了$ Result.content,尽管显然这可以为您提供整个内容部分。有没有一种方法可以从内容中获取单个值而无需沿子字符串路径进行操作?

以下是回应示例:


    {"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"}}

这是我的Powershell代码:

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 ""
  }
}

1 个答案:

答案 0 :(得分:5)

PowerShell内置了对处理JSON的支持,因此让我们通过ConvertFrom-Json运行您的输入,看看会得到什么。

$Result = '{"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"}}' 

$Result = $Result | Convertfrom-json

在您的用法中,只需运行最后一部分即可将$Result转换为PowerShell对象。

$Result有一个名为.transaction的属性,所有信息都存放在该属性中。我们可以使用以下语法来解决这个问题。

$result.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   : 
reference        : D000000516
account_id       : 07b286ad-dabc-42a7-ab1b-5302fd382e6c
tag              : 
end_balance      : 
idempotent_key   : 1f25e9f8-64cf-4e91-b4d3-8753437b6dbb
created_at       : 2019-04-05T13:02:19Z
updated_at       : 2019-04-05T13:02:19Z

因此,现在,如果我们要从那里提取特定值,请运行以下命令。

C:\Users\Stephen> $result.transaction.amount
25000

C:\Users\Stephen> $result.transaction.status
Waiting for funds

C:\Users\Stephen> "Transaction in the amount of $($result.transaction.amount) -- status: $($result.transaction.status)"
Transaction in the amount of 25000 -- status: Waiting for funds

这应该可以帮助您。

最终花絮,如果您将Invoke-WebRequest换成Invoke-RestMethod,则JSON转换将自动为您完成!