I am currently working on creating a loadrunner 'js' file to extract specific data from a json file. The json file is hosted on a web server, which is not causing any issues in retrieving the data. However, my challenge lies in returning a particular value from the json text. I have made an attempt to achieve this on a local html page as shown below, but unfortunately, I keep encountering an error message:
Error encountered: TypeError: Cannot read property 'Location' of undefined
Here is the code snippet used:
<!doctype html> <head></head><script>
function getdata()
{
var jsontext = '{"Locations":{"Location":[{"@id":"3649","@latitude":"51.758","@longitude":"-1.576","@country":"England","@zoomLevel":"10","@obsZoomLevel":"5","@type":"Observing Site","@name":"Brize Norton","@geohash":"gcnyknk2h"},{"@id":"3","@latitude":"50.9503","@longitude":"-1.3567","@country":"England","@zoomLevel":"11","@type":"Airport and Heliport","@name":"Southampton Airport","@geohash":"gcp1c5hp4"}]}}';
var stringifiedjson = JSON.stringify(jsontext).replace(/@/g, '_'); //convert to JSON string
var data = JSON.parse(stringifiedjson);
json.innerHTML=data;
try{
var newlocation0 = data.Locations.Location[1]._id;
output.innerHTML=newlocation0;
return;
}
catch(err){
message.innerHTML = "(1)Error encountered - " + err;
}
try{
var newlocation0 = data.Locations.Location[1]._id;
output.innerHTML=newlocation0;
return;
}
catch(err) {
message.innerHTML = message.innerHTML + "(2)Error encountered - " + err;
}
try{
var newlocation0 = data.Locations.Location[0]._id;
output.innerHTML=newlocation0;
return;
}
catch(err) {
message.innerHTML = message.innerHTML + "(3)Error encountered - " + err;
}
}
</script>
<body>
Error Messages:
<p id="message"></p>
<BR><BR>
JSON Input:
<p id="json"></p>
<BR><BR>
Output:
<p id="output"></p>
<button onclick="getdata()">Check</button>
<button onclick="location.reload();">Reload</button>
</body>
I have experimented with different notations such as [0] and [1], yet I am unable to get the desired output from the json content.