Run checks on an array of JSON objects

I have an array og JSON object in my response body and want to run check on each of the JSON object. Will something like this work in check ? or i have to loop the array and have checks inside the loop ?

 success = check(res, {    
"id contains:": (r) => {
       var arr = JSON.parse(r.body);
        arr.forEach(element => {
            element.id == "test";
        });
   }        
});

Hi @jeevananthank

import http from "k6/http";
import { check } from "k6";

export default function() {
    var url = "https://httpbin.test.loadimpact.com/json"
    var res = http.get(url);
    check(res, {
        "id contains:": (r) => {
            var arr = r.json("slideshow.slides");
            return arr.every(element => {
                console.log(JSON.stringify(element));
                console.log(JSON.stringify(element.type));
                return element.type == "all"
            });
        }
    });
}

In this case, I need to get an array from the json response I have. So you will need to change it, but you need to use every and you probably will need response.json() without a selector if your json is just an array.

1 Like