简体   繁体   中英

How to get particular values from JSON using powershell

I have to get the values from JSON file for my test report purpose. Can anyone help me how get values of passes , failure from the below JSON data.

    {
  "stats": {
    "suites": 2,
    "tests": 4,
    "passes": 3,
    "pending": 0,
    "failures": 0,
    "start": "2021-11-05T15:58:26.817Z",
    "end": "2021-11-05T15:59:26.701Z",
    "duration": 55162,
    "testsRegistered": 4,
    "passPercent": 75,
    "pendingPercent": 0,
    "other": 0,
    "hasOther": false,
    "skipped": 1,
    "hasSkipped": true
  },
}

I have tried below code, but not getting correct result

$json= Get-Content -Raw -Path 'C:\report.json' | ConvertFrom-Json
write-host "passes : $($json.passes)"
write-host "failures : $($json.failures)"

Result:

$json= Get-Content -Raw -Path 'C:\report.json' | ConvertFrom-Json
write-host "passes : $($json.passes)"
write-host "failures : $($json.failures)"
passes : 
failures :

Edit: I have got the solution by adding below code

$json= Get-Content -Raw -Path 'C:\report.json' | ConvertFrom-Json
write-host "passes : $($json.stats.passes)"
write-host "failures : $($json.stats.failures)"

try this

$json= Get-Content -Raw -Path <jsonFile>.json | ConvertFrom-Json
//or
$json = $response | ConvertFrom-Json //if you have it from url


write-host "passes : $($json.passes)"
write-host "failures : $($json.failures)"

I have got the solution by adding below code

$json= Get-Content -Raw -Path 'C:\report.json' | ConvertFrom-Json
write-host "passes : $($json.stats.passes)"
write-host "failures : $($json.stats.failures)"

Result:

passes : 14
failures : 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM