Parsing JSON data in Javascript
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Parsing JSON data in Javascript
I'm trying to sort out how to parse the JSON data from a GET request using an API. The goal is to take the individual fields and save them to Project level variables to use in Property Checkpoints. The code below is the example function from aqHttpResponse Object | TestComplete Documentation (smartbear.com). The thought is that I should be able to use the line in orange down below, as well as other lines of code, to set the individual fields of the response to variables. I probably have the wrong syntax as I'm not super familiar with Javascript. Is it possible to take the data directly from a GET request and set it to a variable, does the data need to be parsed first, or is this not possible at all in TestComplete?
function httpGetRequest()
{
var address = "http://httpbin.org";
// Create an aqHttpRequest object
var aqHttpRequest = aqHttp.CreateGetRequest(address);
// Send the request, get an aqHttpResponse object
var aqHttpResponse = aqHttpRequest.Send();
if (aqHttpResponse != null)
{
// Read the response data
Log.Message(aqHttpResponse.AllHeaders); // All headers
Log.Message(aqHttpResponse.GetHeader("Content-Type")); // A specific header
Log.Message(aqHttpResponse.StatusCode); // A status code
Log.Message(aqHttpResponse.StatusText); // A status text
Log.Message(aqHttpResponse.Text); // A response body
// This is the test line, likely the wrong syntax
Project.Variables.Name = aqHttpResponse.Text.Name;
// Save the response body to a file and place it to the project folder
aqHttpResponse.SaveToFile(Project.Path + "body.txt");
}
}
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
You've posted this in the ReadyAPI forum instead of the TestComplete forum. I haven't used TestComplete, but I've done a reasonable amount of Javascript.
The first first I would hone in on are these lines....
Log.Message(aqHttpResponse.StatusCode); // A status code
Log.Message(aqHttpResponse.StatusText); // A status text
Log.Message(aqHttpResponse.Text); // A response body
Did you get a 200 for status code? Did you get a response? Apologies if you know all this already. If you got a 200, you know the service call worked. If you can see that response body in the logs, then it probably is the orange line.
What I cannot tell from the example is whether the service call is asynchronous or synchronous (have a Google on "javascript Fetch API"). It might be that you need to 'await' the response before trying to log it.
Lastly, JSON is case-sensitive and quite often data is stored within an array. If you see the data you want to access in square brackets, you need to access it slightly differently. If you could post an excerpt of the JSON, I might be able to help. Beyond that, heopfully someone with more TestComplete experience will be able to reply.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I'm using a MLB API for the time being instead of the API for our billing site. I wanted to make sure it could be done before I worked on trying to make it work with our expected data. The JSON data I received was:
{"search_player_all":{"copyRight":" Copyright 2022 MLB Advanced Media, L.P. Use of any content on this page acknowledges agreement to the terms posted here http://gdx.mlb.com/components/copyright.txt ","queryResults":{"created":"2022-05-25T21:28:12","totalSize":"1","row":{"position":"2B","birth_country":"USA","weight":"195","birth_state":"CA","name_display_first_last":"Jeff McNeil","college":"Long Beach State","height_inches":"1","name_display_roster":"McNeil","sport_code":"mlb","bats":"L","name_first":"Jeff","team_code":"nyn","birth_city":"Santa Barbara","height_feet":"6","pro_debut_date":"2018-07-24T00:00:00","team_full":"New York Mets","team_abbrev":"NYM","birth_date":"1992-04-08T00:00:00","throws":"R","league":"NL","name_display_last_first":"McNeil, Jeff","position_id":"4","high_school":"Nipomo, CA","name_use":"Jeff","player_id":"643446","name_last":"McNeil","team_id":"121","service_years":"","active_sw":"Y"}}}}
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Assuming
Log.Message(aqHttpResponse.Text);
Logs the response you've pasted above, to access name try....
Log.Message(aqHttpResponse.Text.search_player_all.queryResults.row.name_display_roster);
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
When trying to use
Log.Message(aqHttpResponse.Text.search_player_all.queryResults.row.name_display_roster);
I get an error message. The log shows a 200 response from the query and shows the body of the response just as I pasted it in my previous message.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
It's a bit difficult drilling into this now as I'm UK-based. I can try and look on Monday, but in the meantime, try building the path incrementally....
Log.Message(aqHttpResponse);
Log.Message(aqHttpResponse.Text);
Log.Message(aqHttpResponse.Text.search_player_all);
Log.Message(aqHttpResponse.Text.search_player_all.queryResults);
I also wonder whether the response has come back as a string and firstly needs to be cast to a JSON Object. Thinking about it, that is likely.
From W3 (https://www.w3schools.com/js/js_json_parse.asp)....
const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}');
console.log(obj.name);
You could try....
const myObj = JSON.parse(aqHttpResponse.Text); // Cast a string to JSON Object.
Log.message(myObj.Text) // If you get a response here, we're winning.
// Then try and navgigate through your object.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
A combination of the two worked beautifully.
const hopefulParse = JSON.parse(aqHttpResponse.Text);
var position = hopefulParse.search_player_all.queryResults.row.position;
Log.Message(position);
Thank you Chris.
