cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Postman : filter the output of a GET request

Conversationalist

Postman : filter the output of a GET request

Hello,

 

I'd like to filter the list of the networks I get from GET  {{baseUrl}}/organizations/{{organizationId}}/networks using the tags applied to networks, in postman.

 

For example, I would like to keep all the networks where the "tags" field contains "KIT", in the following JSON sample, the network "redacted3" would not be included

 

From what i get, I should use javascript in the test section, but i'm so bad at this for now, i would love some help for this first script.

 

Sample Json :

{
        "id": "redacted1",
        "organizationId": "xxx",
        "name": "Location1",
        "timeZone": "Europe/Paris",
        "tags": " KIT0003 OSI ",
        "type": "combined",
        "disableMyMerakiCom": false,
        "disableRemoteStatusPage": false
    },
    {
        "id": "redacted2",
        "organizationId": "xxx",
        "name": "Location2",
        "timeZone": "Europe/Paris",
        "tags": " KIT0006 OSI ",
        "type": "combined",
        "disableMyMerakiCom": false,
        "disableRemoteStatusPage": false
    },
    {
        "id": "redacted3",
        "organizationId": "xxx",
        "name": "Location3",
        "timeZone": "Europe/Paris",
        "tags": " OSI ",
        "type": "combined",
        "disableMyMerakiCom": false,
        "disableRemoteStatusPage": false

 

 

What it would look like if it was outside postman, done in ugly python :

 

#list networks with specific tag (KIT for example) (python)
import json

with open('network_list.json') as data_file:
    data = json.load(data_file)
new_network_list = []
for i in data:
        if "KIT" in str(i["tags"]):
                filtered_network_list.append(i)
f = open( 'output.json', 'w' )
f.write(str(filtered_network_list))
f.close()

4 REPLIES 4
Meraki Employee

Re: Postman : filter the output of a GET request

This is the JS solution to filtering the JSON array based on network tags within Postman. I'm not clear how you want to use the data, so I am just printing it to the console. (Menu -> Developer --> Show DevTools (CurrentView) )

var jsonData = JSON.parse(responseBody);
var filteredData = jsonData.filter(function(item) {
  if (!item.tags) {
    return
  }
  return item.tags.includes("KIT");
});
console.log("filteredData", filteredData);

 

My Local Test

Screen Shot 2019-03-07 at 2.20.45 PM.pngScreen Shot 2019-03-07 at 2.24.29 PM.png

 

 

 

 

As a more generic HTML/JS solution

var data = [{
    "id": "redacted1",
    "organizationId": "xxx",
    "name": "Location1",
    "timeZone": "Europe/Paris",
    "tags": " KIT0003 OSI ",
    "type": "combined",
    "disableMyMerakiCom": false,
    "disableRemoteStatusPage": false
  },
  {
    "id": "redacted2",
    "organizationId": "xxx",
    "name": "Location2",
    "timeZone": "Europe/Paris",
    "tags": " KIT0006 OSI ",
    "type": "combined",
    "disableMyMerakiCom": false,
    "disableRemoteStatusPage": false
  }, {
    "id": "redacted3",
    "organizationId": "xxx",
    "name": "Location3",
    "timeZone": "Europe/Paris",
    "tags": " OSI ",
    "type": "combined",
    "disableMyMerakiCom": false,
    "disableRemoteStatusPage": false
  }
]



newData = data.filter(function(item) {
  if (!item.tags) {
    return
  }
  return item.tags.includes("KIT");
});
console.log(newData);

//Print to HTML page


document.getElementById("demo").innerHTML = "<pre>"+JSON.stringify(newData,undefined, 2) +"</pre>"

 

Results

[
  {
    "id": "redacted1",
    "organizationId": "xxx",
    "name": "Location1",
    "timeZone": "Europe/Paris",
    "tags": " KIT0003 OSI ",
    "type": "combined",
    "disableMyMerakiCom": false,
    "disableRemoteStatusPage": false
  },
  {
    "id": "redacted2",
    "organizationId": "xxx",
    "name": "Location2",
    "timeZone": "Europe/Paris",
    "tags": " KIT0006 OSI ",
    "type": "combined",
    "disableMyMerakiCom": false,
    "disableRemoteStatusPage": false
  }
]

 

Here is a working JS fiddle to demo it.
https://jsfiddle.net/oa86k3ug/

 

Hope this helps,

Cory

Conversationalist

Re: Postman : filter the output of a GET request

wow thanks, i'm nearly there thanks to you.

 

To add precisions, I was trying to have my result go in the "Tests Results" section, like those samples would do : https://blog.getpostman.com/2017/10/25/writing-tests-in-postman/

 

Do you know if it's feasible ? I'm really not sure.

 

Anyway your code works great !!

Meraki Employee

Re: Postman : filter the output of a GET request

The "test results section" will simply return a status success/failed with a message string.

 

If you are wanting to use the resulting data response, you could set the data to an environment variable. 

 

More Info: https://learning.getpostman.com/docs/postman/scripts/test_examples/

 

var jsonData = JSON.parse(responseBody);
var filteredData = jsonData.filter(function(item) {
  if (!item.tags) {
    return
  }
  return item.tags.includes(" Teleworkers");
});
console.log("data", filteredData);

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
    pm.environment.set("filteredNets", JSON.stringify(filteredData));
});

After running my test, the new Env variable showed up and had my data (from the sandbox org)
Screen Shot 2019-03-07 at 4.01.08 PM.png

Screen Shot 2019-03-07 at 4.02.08 PM.png

Meraki Employee

Re: Postman : filter the output of a GET request

FWIW, I tried sending the data directly to the test results, but it looks kind of silly.

 

Screen Shot 2019-03-07 at 4.22.44 PM.png

Welcome to the Meraki Community!
To start contributing, simply sign in with your Cisco account. If you don't yet have a Cisco account, you can sign up.