[Newbie] How to filter values displayed in JSON data?

Hello there! How are you doing?

I would like to apologize beforehand about this newbie question, i know it might seem silly and kind of obvious, but i really do not know on how to do this.

I’m currently doing a query on a API that gets almost 40000 lines of data - 20 rows in total (So, there is a lot of columns and data…)

Here’s a example of one structure (Just the beginning):

{
    "meta": {
        "rc": "ok"
    },
    "data": [
        {
            "_id": "5c83d1779959a00f94014055",
            "ip": "192.168.25.66",
            "mac": "12:d9:34:be:56:e8",
            "model": "BZ2",
            "type": "uap",
            "version": "4.3.28.11361",
            "adopted": true,
            "site_id": "1233b3456959678f94910b93",
            "cfgversion": "xxxcd29xxx262xxx",
            "config_network": {
                "type": "dhcp",
                "ip": "192.168.1.1"
            },
            "vwire_table": [],
            "dot1x_portctrl_enabled": false,
            "license_state": "registered",
            "two_phase_adopt": false,
            "unsupported": false,
            "unsupported_reason": 0,
            "inform_url": "http://unifi:8080/inform",
            "inform_ip": "192.168.1.1",
            "serial": "4EE9E7EE01EE",
            "required_version": "2.4.4",
            "board_rev": 18,
            "antenna_table": [
                {
                    "default": true,
                    "id": 4,
                    "name": "Combined",
                    "wifi0_gain": 3
                }
            ],
            "radio_table": [
                {
                    "radio": "ng",
                    "name": "wifi0",
                    "ht": "20",
                    "channel": 6,
                    "tx_power_mode": "medium",
                    "antenna_gain": 6,
                    "min_rssi_enabled": false,
                    "sens_level_enabled": false,
                    "vwire_enabled": true,
                    "wlangroup_id": "zxcvb3169000a00f9401vcxz",
                    "min_txpower": 5,
                    "max_txpower": 23,
                    "builtin_antenna": true,
                    "builtin_ant_gain": 3,
                    "current_antenna_gain": 0,
                    "nss": 2,
                    "radio_caps": 16404
                }
            ],
            "scan_radio_table": [],
            "ethernet_table": [
[... Etc]

So, what i want to do is:
Just display the following values, of the 20 objects with lots of data

→ “ip”
→ “mac”

Thats it.

Can someone give me some light, please?

Something like. The does iterate through the response so if you have 40000 lines of data.

const response = pm.response.json();
for (var {ip: i, mac: m} of response.data) {
  console.log('ip: ' + i + ', mac:' + m);
}

I tried using map and other Javascript functions, but failed miserably to make this simpler. I can pull back a single entry easy enough, but not multiple elements. This does appear to be a Javascript issue rather than postman. So if there are any Javascript developers at hand.

Hi @RaulChiarella

@mdjones’s answer is clever, I like that!!
And assuming the data is correct it does indeed pull back multiple values (I added in some additional objects to test).

image


An alternative/easier-to-read way to write the for loop (for beginners like me) would be like this;

for (i = 0; i < response.data.length; i++){
    let ipAddress = response.data[i].ip;
    let macAddress = response.data[i].mac;
    console.log('ip: ' + ipAddress + ', mac: ' + macAddress);
}

That said, I really do like Mike’s answer!! … And will be using that way myself in the future!

This is probably just going to confuse things but you could map those values to a new array like this:

let mappedResults = pm.response.json().data.map(({ ip, mac }) => ({ ip, mac }))
console.log(mappedResults)

@dannydainton

I think that was what I was trying to do when I was playing around with the MAP feature, but had the formatting slightly wrong for the array.

As Mando say’s. This is the way :slight_smile: