Parsing JSON to extract values for further Parameterization & Correlation - Test Automation with Visual Studio
JSON (JavaScript Object Notation) is a light weight Data-Interchange in Text format completely Language independant. It is used as a alternative to using XML, though both are structured approaches to mark up data. JSON like XML, has a structure with nesting of Data Elements. Both are Text based and both use Unicode. But the subtle benefits are - JSON is more clean and a little less verbose. Each instance of a JSON document describes one object with nested objects, arrays, strings, numbers, boolean values, or null values. Application may prefer using JSON over a XML structure since it is a subset of Javascript and you can use JavaScript's own compiler to do the parsing. Also, navigating an object synthesized from JSON is identical to navigating any JavaScript object. It's far easier than navigating through the DOM tree.
An object, in JSON terminology, is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
VSTS is able to record and playback Ajax/JSON requests. However, there's no built-in support for doing things like Validating the contents of a JSON Response or extracting values if needed.
In the above XML DOM structure, the this is the JSON text.
Inside the Extract method, read the innertext property for the XML Node that contains the JSON instance, load the text to an XML Document Object, and then further using XPATH expression to reach the particular *Keys* , parse out the values for the same.
You can further learn about writing custom extraction Rules
Approach for parameterizing.
To parameterize the UserName from the above json text
Approach to parameterize “UserName”
Create a Context parameter in the WebTest which hold the value. In this case I created a Context parameter for UserName.