0

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)"
1
  • 2
    The json you show is invalid (the final comma after the closing curly bracket should not be there) or is this just part of the file? If so, please show us a sanitized version where the complete structure of the json data can be seen Commented Nov 7, 2021 at 20:58

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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.