1

I have a response body that looks like this

{
   "data_sets":[
    { 
           "author": {
               "name": "foo"
           },
           "category": "zip",
           "client": true,
           "data_type": "zip",
           "index": "completed",
           "id": 200,
           "params": {}
    },
    {
           "author": {
               "name": "foo2"
           },
           "category": "zip",
           "client": true,
           "data_type": "zip",
           "index": "completed",
           "id": 190,
           "params": {}
    }]}

I am trying to iterate over my thousands of datasets in my postman test and just simply console.log the id that equals the id in my environmental variable. For this case, let's say that is 190. I'm trying something like this but it isn't working. It isn't failing but it's not giving me my expected output.

Here is my code in postman

    pm.test("the response has the right id", () => {
    id_dataset = pm.environment.get("id");
    var jsonData = pm.response.json();
    for(var i = 0; i < jsonData.data_sets.length; i++)
   if(jsonData.data_sets[i].id == id_dataset)
     console.log(jsonData.data_sets[i].id);
    });

Can anyone help with this?

  • 3
    `i < jsonData.data_sets.length` (Note that you can also replace the loop with `const data_set = jsonData.data_sets.find(item => item.id === id_dataset);`) –  Apr 14 '21 at 18:14
  • (On an unrelated note: the server response is text in JSON format. `pm.response.json();` parses that JSON text, which results in an object. This object in turn contains `.data_sets`, which is an array. There's no such thing as a "JSON array" or "JSON object".) –  Apr 14 '21 at 18:22
  • The JSON is invalid. It's missing some commas, the second element is missing `}`. – Barmar Apr 14 '21 at 18:35
  • @Shultz please check my solution using the array method `find()` and let me know if this works for you. – Brandon McConnell Apr 14 '21 at 18:56
  • I've added optional chaining `?.id` to my solution to prevent an error from being thrown whenever the ID is not present. – Brandon McConnell Apr 14 '21 at 19:21

2 Answers2

1

We should be able to use find() to quickly search through all the records and find the record with the matching id of 190. Try this:

pm.test("the response has the right id", () => {
    id_dataset = pm.environment.get("id"); // 190
    var jsonData = pm.response.json();
    console.log(jsonData.data_sets.find(set => set.id === id_dataset)?.id);
});

Here is the same logic in vanilla JS:

let id_dataset = 190;

let data_sets = [ { "author": { "name": "foo" }, "category": "zip", "client": true, "data_type": "zip", "index": "completed", "id": 200, "params": {} }, { "author": { "name": "foo2" }, "category": "zip", "client": true, "data_type": "zip", "index": "completed", "id": 190, "params": {} }];

console.log(data_sets.find(set => set.id === id_dataset)?.id);
Brandon McConnell
  • 5,776
  • 1
  • 20
  • 36
  • @Shultz would you be free to chat about this? It looks like you may have resolved your issue but I would still like to test with my example as I think it will simplify your logic overall: https://chat.stackoverflow.com/rooms/231173/question-67096991 – Brandon McConnell Apr 15 '21 at 13:12
0

You are forgetting .length inside your for

  const jsonData = {data_sets: [ { "author": { "name": "foo" }, "category": "zip", "client": true, "data_type": "zip", "index": "completed", "id": 200, "params": {} },{ "author": { "name": "foo" }, "category": "zip", "client": true, "data_type": "zip", "index": "completed", "id": 200, "params": {} },{ "author": { "name": "foo" }, "category": "zip", "client": false, "data_type": "zip", "index": "completed", "id": 200, "params": {} }, { "author": { "name": "foo2" }, "category": "zip", "client": true, "data_type": "zip", "index": "completed", "id": 190, "params": {} }]};

 const id_dataset = 200;
 for(var i = 0; i < jsonData.data_sets.length; i++)
   if(jsonData.data_sets[i].id == id_dataset)
     console.log(jsonData.data_sets[i].id);
  

you can use map too:

const jsonData = {data_sets: [ { "author": { "name": "foo" }, "category": "zip", "client": true, "data_type": "zip", "index": "completed", "id": 200, "params": {} },{ "author": { "name": "foo" }, "category": "zip", "client": true, "data_type": "zip", "index": "completed", "id": 200, "params": {} },{ "author": { "name": "foo" }, "category": "zip", "client": false, "data_type": "zip", "index": "completed", "id": 200, "params": {} }, { "author": { "name": "foo2" }, "category": "zip", "client": true, "data_type": "zip", "index": "completed", "id": 190, "params": {} }]};

const id_dataset = 200;

 jsonData.data_sets.map(item=>item.id==id_dataset && console.log(item.id))
  • 1
    Use `forEach` rather than `map` if you're not using the result. – Barmar Apr 14 '21 at 18:36
  • I changed my code to have those updates but there is still no output in the logs –  Apr 14 '21 at 18:47
  • if you want to see a console.log for testing purposes only, it's better to write less code – Martin Gainza Koulaksezian Apr 14 '21 at 18:48
  • @Shultz could you provide the result of `console.log(jsonData)`? proabably you need to parse the response with `.json()` – Martin Gainza Koulaksezian Apr 14 '21 at 18:50
  • my end goal is to assert that lets say id of 190 has a "client":true, but that wasn't working so I'm going step by step and figuring out each part –  Apr 14 '21 at 18:50
  • @MartinGainzaKoulaksezian when I do console.log(jsonData) nothing shows up in the console. –  Apr 14 '21 at 18:52
  • end goal is something like pm.assert(jsonData.data_sets[i].client).to.eq_true); only if the id = 190 which is must iterate through the data_sets array to find –  Apr 14 '21 at 18:54
  • to do that you just have to add another if inside the `for`, it is strange that `jsonData` does not print anything. you can try doing the test instead of a console.log – Martin Gainza Koulaksezian Apr 14 '21 at 19:03
  • @MartinGainzaKoulaksezian you code snippet worked, however, any ideas why any other json object doesn't work such as console.log(jsonData.data_sets[i].client); –  Apr 14 '21 at 19:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/231142/discussion-between-shultz-and-martin-gainza-koulaksezian). –  Apr 14 '21 at 19:32
  • it should works! if not probably the jsonData structure is different against the structure that you posted – Martin Gainza Koulaksezian Apr 14 '21 at 19:32