Search code examples
jsonvbscript

How to get value from JSON file to a variable?


When I run the protractor test case, I got the below JSON response for the test case result, which get stored in a file as combined.json.

Case 1: For passed test case:

{"description":"tests the case when the login was successful|E2E test Login and search users with different search criteria",
"passed":true,
"os":"XP",
"browser": chrome
}

Case 2: For Failed test case:

{"description":"tests the case when the login was successful|E2E test Login and search users with different search criteria",
"passed":false,
"os":"XP",
"browser": chrome
}

In above cases test result is stored in the passed JSON object.

I am writing a VAPI test case in HPALM and for that I need to pass the test case status. I would like to get the value of passed to a variable.


Solution

  • I don't think there's a JSON parser for VBScript, so you'll have to parse the file yourself. Since you have a very simple scenario (extract the value for a specific key) using a regular expression should be fine, though:

    Set fso = CreateObject("Scripting.FileSystemObject")
    
    json = fso.OpenTextFile("C:\path\to\combined.json").ReadAll
    
    Set re = New RegExp
    re.Pattern = """passed"":(true|false),"
    re.IgnoreCase = True
    
    For Each m In re.Execute(json)
      passed = CBool(m.SubMatches(0))
    Next