Need help pasrsing api response
Hi all,
I'm new to testcomplete and I'm assigned to write test scripts using jscript for some API testing. For one of the test, I need to parse JSON response and retrive a key of some sort and save it. I can't seems to wrap my hand around it and figure it out. Any help would be appreciated.
Following is API response -
{
"date":"2016-12-31T00:00:00.0000000",
"accountid":"8675309",,
"license_mode":"mode1",
"user_email":"primaryuser@gmail.com",
"key":"0efca74a-7f9b-41f8-96a9-b05a3367854a",
}
and I need to retrive the key and save it for later use.
function Get_Key (responsetxt)
{
var name = "key"
var qs = dotNET.System_Web.HttpUtility.ParseQueryString(responsetext);
var strValue = qs.Item(name).OleValue;
}
that's all I have so far.
Thank you very much for taking time out to help me.
Basically the JSON.parse is a built in Jscript function that goes through the response text formatted as JSON. and creates an array of all the objects in it with their properties. Your response only has one object with several properties one of the properties being 'key'.
So after it has been parsed you have an array called json, with one object at position 0, or json[0], the porpety is accessed by it's name 'key'.
More reading that might help clear it up more as well:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
http://www.w3schools.com/js/js_objects.asp
http://www.w3schools.com/js/js_json.asp
http://www.w3schools.com/js/js_arrays.asp