Checking values of multiple properties of a JSON output

I have couple of issues as below. I tried couple of times but it is not working. I would appreciate your help and please also recommend the best way to get rid of the errors.

JSON response
[
{
“studentName”: “Mike Tucker”,
“startDate”: “2011-08-08 12:12:16.257”,
“isActive”: “true”,
“statusDescription”: “Pass”,
“studentId”: “39524”
},
{
“studentName”: “James Brown”,
“startDate”: “2012-11-06 12:15:12.72”,
“isActive”: “true”,
“courseDescription”: “Fail”,
“studentId”: “52412”
}
]

I would like to know the ‘courseDescription’ value is either “Pass” or “Fail” from the above json output

I tried the following script but it is not working

var jsonData = pm.response.json();
var courseDescription =[‘Pass’,‘Fail’];
pm.test(“courseDescription’ is working as expected”,()=>{
pm.expect(‘Active)’).to.be.oneOf(‘courseDescription’));
});

Error : AssertionError: expected ‘courseDescription’ to be an array

I would like to know the date format above output is YYYY-MM-DD with the script below for all json objects

I tried the script below and it is not working
var josnData= pm.response.json();
var moment = require(‘moment’);
var startDate= ‘YYYY-MM-DD’;
pm.test(“Checking date format”, () =>
{
pm.expect(jsonData.data.startDate).to.include(moment).format(startDate)});

Error : ReferenceError: jsonData is not defined

I would like to check the studentID is an integer for all the json objects and length of it .i.e in this case stuedntID length is 5 digit. I tried the following script ,please let me know the piece I am missing

AssertionError: expected undefined to be a number
var data = pm.response.json();
pm.test(“studentID is integer and be below 6 digit”, function () {
pm.expect(data.studentID).to.be.a(“number”);
pm.expect(data.studentID).be.below(6);
});
Error : AssertionError: expected undefined to be a number

I would like to check details of studentID # 52412 from all the json output above. I tried the script below and need your suggestions to work

var jsonData = pm.response.json();
pm.test(‘studentID 52412’, function() {
pm.expect(jsonData).to.have.property(jsonData.data.studentID==52412’);
});

Please also refer any good json libraries that are helpful for me to learn for future.

I’ve already tried:

That is four questions in one. Perhaps start with one at at time.

The first point I would make is that as soon as you parse the response using pm.response.json() you are no longer working with JSON but a JavaScript object so learning basic JavaScript and how to work with objects and arrays will be helpful. (The WC3 schools is a good place to start).

When you post code, can you please use the Preformatted text option in the editor so everything isn’t aligned to the left (which makes it hard to read).

Lets start with the first question and go from there.

pm.expect(‘Active)’).to.be.oneOf(‘courseDescription’));

Apart from the two extra “)”, you’ve wrapped the courseDescription in quotes, so its reading this as a string. Remove the quotes. At the moment, its just comparing the word “active” with the word “courseDescription” not the variables of those names. OneOf needs an array so its failing at that point. (It will also fail because the variable “active” doesn’t appear to be set anywhere in your code.

Your response will produce an array of JavaScript objects\records when its parsed. Which record do you want to assert against? You seem to have multiple students.

Why is one record showing “statusDescription” but the other is showing “courseDescription”. Is this just a typo?

let myVariable = "Pass";    
let expectedResult = ["Pass", "Fail"];
    
pm.test("Pass or Fail", () => {
    pm.expect(myVariable).to.be.oneOf(expectedResult);
});

I would recommend going through the Postman training if you haven’t done so yet.

Postman Galaxy Training | Postman API Network

Run through the “APIs 101 Training”, then the “Testing and Automation Training” course.

Your code for checking dates is just wrong. Please Google “javascript moment” and you will see the correct format for using this library. You can also search this forum for examples. Try this first and then post that as a separate question and you more more likely to get a response.