Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postman get value from JSON where equals a value in array using javascript

Currently using the latest version of Postman: 6.7.4 (Latest)

I'm trying to get a value out of a JSON response body and store it in an environment variable BUT the value 'username' should be equal to my preferred username.

Normally I would extract a value like this:

var jsonData = pm.response.json();
pm.environment.set("useridToken", jsonData.Customers[0].userid);

This would give me the first item in the list but I do not wish to obtain the first nor the second item from the list. I wish to obtain the userid where username EQUAL "Billy" for example.

Output of the body response:

{
"Customers": [
    {
        "id": 24,
        "userid": 73063,
        "username": "BOB",
        "firstname": "BOB",
        "lastname": "LASTNAME
    },
    {
        "id": 25,
        "userid": 73139,
        "username": "Billy",
        "firstname": "Billy",
        "lastname": "lasty"
    }
   ]
}

Any tips?

I remember in SoapUI it was like this:

$.channels[?(@.is_archived=='false')].id[0]

I guess it's not possible to do this in JS in Postman?

like image 388
Decypher Avatar asked Feb 27 '19 09:02

Decypher


People also ask

Can a JSON value be an array?

Arrays in JSON are almost the same as arrays in JavaScript. In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.


2 Answers

This answer is inspired by the other answer that outputs an array. 1

It is not clearly stated by the original poster whether the desired output should be a single userid (presumably the first occurence?) - or an array containing all userid:s matching "Billy".

This answer shows a solution to the latter case by using Lodash.

const jsonData = {
  Customers: [{
    userid: 73063,
    username: 'BOB'
  }, {
    userid: 73138,
    username: 'Billy'
  }, {
    userid: 74139,
    username: 'Billy'
  }]
};

const userIds = [];
_.forEach(_.filter(jsonData.Customers, c => c.username === 'Billy'),
  item => { userIds.push(item.userid); });
console.log(userIds);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.19/lodash.js"></script>

1 That answer is quite helpful as it hints how to filter out the relevant objects of the Customers array. However, the original poster wants (an array of) the userid(s) which is a number, and not an an array of objects that contains the userid:s. This is how my answer here is different.

like image 159
Henke Avatar answered Oct 25 '22 09:10

Henke


You can use: Array.prototype.find():

const data = {
  "Customers": [{
      "id": 24,
      "userid": 73063,
      "username": "BOB",
      "firstname": "BOB",
      "lastname": "LASTNAME"
    },
    {
      "id": 25,
      "userid": 73139,
      "username": "Billy",
      "firstname": "Billy",
      "lastname": "lasty"
    }
  ]
}

const user = data.Customers.find(u => u.username === 'Billy')
const userid = user ? user.userid : 'not found'

console.log(user)
console.log(userid)
like image 24
Yosvel Quintero Arguelles Avatar answered Oct 25 '22 11:10

Yosvel Quintero Arguelles