'How do I search for a particular object within an array of JSON objects using postman

How do I query for a particular BusStopCode from within a JSON object in a JSON array

 "value": [
        {
            "BusStopCode": "01012",
            "RoadName": "Victoria St",
            "Description": "Hotel Grand Pacific",
            "Latitude": 1.29684825487647,
            "Longitude": 103.85253591654006
        },
        {
            "BusStopCode": "01013",
            "RoadName": "Victoria St",
            "Description": "St. Joseph's Ch",
            "Latitude": 1.29770970610083,
            "Longitude": 103.8532247463225
        },
       

for example if I want to find only the first object then the bus stop code I would query is 01012

my current URL query request looks like this- http://transport/dataservice/BusStops?BusStopCode=01012

here http://transport/dataservice/BusStops is my URL

and ?BusStopCode=01012 is my path



Solution 1:[1]

You can filter your response data using JSONpath Visualizer in Postman.

To enable the Visualizer, please paste the following code in the Test section of Postman where you are creating your request.

let template = `
<html>
<head>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/jsonpath.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
</head>
<body>
    <div>
      <div>
        <input id="filter" style="width:450px;" type="text" placeholder="Example query: $..name.first">
      </div>
      <div>
        <button id="resetButton" style="background-color:red;color:white;">Reset</button>
        <input id="showErrors" type="checkbox" value="1"/>
        <span class="item-text" style="font-family:courier;">Show Evaluation Errors</span>
      </div>
      <div id="errors" style="font-family:courier;color:red;display:none;"></div>
      <div>
        <p id="content" style="font-family:courier;color:green;font-size:18px;"></p>
      </div>
    </div>
</body>
</html>

<script>
pm.getData( (error, value) => { 
    const extractedData = jsonpath.query(value, '$');
    
    $(function() {
        $('#filter').keyup(function() {
            try {
                let filteredData = jsonpath.query(extractedData, $(this).val());
                $("#content, #errors").empty();
                $("#content").append("<pre><code>" + JSON.stringify(filteredData, null, 4) + "</code></pre>");
            } catch (err) {
                console.info(err);
                $("#errors").empty();
                $("#errors").append("<pre><code>" + err + "</code></pre>");
            }
        });
    });
    
    $( "#resetButton" ).click(function() {
        $("#content, #errors").empty();
        $("#filter").val('');
        $("#content").append("<pre><code>" + JSON.stringify(extractedData, null, 4) + "</code></pre>");
    })
})

$(function() {
  $("#showErrors").on("click",function() {
    $("#errors").toggle(this.checked);
  });
});
</script>`

pm.visualizer.set(template, pm.response.json())

Here is the example.

enter image description here

Sample data is also provided for better understanding.

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Muhammad Tariq