My question:
Good morning, I am trying to run a scheduled collection report request with a test script that uses a pm.response.json() variable. In the regular API section the test script runs fine, but here I get the “JSONError: No data, empty input at 1:1 ^” error. Is there another way to set up the variable to get the array of information I am pulling from the request?
Details (like screenshots):
The first two pictures are from the collections, while the last one is from the API. I don’t know if I have to change the way I get into the array of information or if there is some work around for the .json response. I would appreciate the help.
JSONError: No Data, empty input at 1:1
This is caused by the response not being in a JSON format.
This is because you are getting a 401 unauthorised error.
This will result in a blank response instead of a JSON object, so the first line of your script where you parse the response will fail with the error you are seeing.
On a side note. You parse the response to the “jsonData” object. You then stringify it, and parse it again to another variable called “result”. The “result” variable will be the same as “jsonData”. object. Why can’t you just use the original jsonData object?
What does your raw data look like? You might be able to search the data for the email addresses instead of looping through each entry. It’s all according to how the data is being returned though.
Thank you for your response. I guess now my question leads into why does the collection give me a 401? It is all the same api and user access keys, so their shouldn’t be any problem. For the way my code is currently, that was the only way I could get into the array that was my raw data. Anything else I used didn’t work.
If you have another way to get into the array of raw data, I am happy to hear it. Thank you again for your response.
How are you currently authenticating the GET request? What does the API require?
Your response is an array of objects, so you can certainly filter on the email addresses to bring back the records that contain that address. (Filter rather than find as it appears that an email address may be enrolled in more than once course).
Postman uses JavaScript under the hood, so after you parse the response, you should be able to manipulate the data using JavaScript functions to your hearts content.
JavaScript Array filter() Method (w3schools.com)
Considering the following response.
[
{
"userEmail": "[email protected]",
"learningPathName": "Course1"
},
{
"userEmail": "[email protected]",
"learningPathName": "Course2"
},
{
"userEmail": "[email protected]",
"learningPathName": "Course3"
},
{
"userEmail": "[email protected]",
"learningPathName": "Course4"
}
]
You can search the elements using the following. (Which will return an array of results).
const response = pm.response.json();
console.log(response);
let search1 = (response.filter(element => {return element.userEmail === '[email protected]'}));
console.log(search1);
let search2 = (response.filter(element => {return element.userEmail === '[email protected]'}));
console.log(search2);
Here are the corresponding console logs.
I will try out your suggestion for the JavaScript. In terms of the GET request all that is needed is the API key, the user access key and the start date, for this particular request. Or I should say all that was given to me were those two things.
Side note, I realized I used some actual emails, do you mind changing them to user1 and user2 respectively. Thank you.
I’ve removed the emails from the post.
In your working GET request. How have you stored the API and access keys?
Headers? Parameters? Environment variables?