Using a variable key to retrieve response value in Postman

After setting up a GET request in Postman that retrieves JSON data, I'm looking to extract the values for keys containing a specific substring.

Here's the code snippet I've been testing:

var jsonData = pm.response.json();
var keys = Object.keys(jsonData);

for(var i = 0; i < keys.length; i++) 
{
    if(keys[i].includes("_number"))
    { 
       console.log(jsonData[keys[i]]);
    }
}

Edit: It seems that the issue lies not in identifying the specified substrings, but in retrieving the corresponding values. Accessing a value directly by key (e.g., jsonData.Id) works without issues, yet using a variable seems to be causing trouble.

Answer №1

To verify, follow these steps:

let responseData = pm.response.json();
let dataKeys = Object.keys(responseData);

for(let j = 0; j < dataKeys.length; j++) 
{
    if(dataKeys[j].includes('_num'))
    { 
       console.log(responseData[dataKeys[j]]);
    }
}

Answer №2

Utilizing the has() method is specifically designed to locate an exact key within a map. To search for a substring, consider this alternative approach:

if(keys[i].indexOf("_number") !== -1)
{ 
   console.log(jsonData.keys[i]);
}

An additional option is to use the includes() function:

if(keys[i].includes("_number"))
{ 
   console.log(jsonData.keys[i]);
}

Answer №3

Another approach can be used to achieve similar results when checking data in the Postman Console:

For a slightly modified technique, you could iterate through jsonData using _.each and examine each item as follows:
    
_.each(Object.entries(jsonData), (item) => {
    if(item[0].includes('_number')) {
        console.log(item[1])
    }
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What's the secret to achieving Apple's signature website font style?

Apple cleverly incorporates a font on its website that appears to have a white shadow, giving it an engraved or etched look. I've come across some techniques, but they seem quite limited. (By the way, a similar effect is utilized on this website as we ...

What are the steps to correctly shut down an ExpressJS server?

I am facing a challenge with my ExpressJs (version 4.X) server - I need to ensure it is stopped correctly. Given that some requests on the server may take a long time to complete (1-2 seconds), I want to reject new connections and wait for all ongoing req ...

What is the reason for JSHint alerting me about utilizing 'this' in a callback function?

While working in my IDE (Cloud 9), I frequently receive warnings from JSHint alerting me to errors in my code. It is important to me to address these issues promptly, however, there is one warning that has stumped me: $("#component-manager tr:not(.edit-co ...

Is there a way to use JQuery to make one button trigger distinct actions in two different divs?

For instance: Press a button - one div flies off the screen while another div flies in. Press the button again - Same div flies out, other div returns. I'm completely new to Javascript/JQuery so any assistance would be highly appreciated! Thank you ...

Effortlessly launching numerous links simultaneously in new tabs

I have written some PHP code: foreach($moduid as $k=>$mod) { $random = $k+1; echo '<a href="http://mysite.com?y='.$cid.'&t='.$mod.'&s=-2" data-pack="true" id="link'.$random.'">data</a>'; } A ...

Having trouble generating a production build for React using Webpack

I have set up webpack and babel to handle JSX and create a minified production build. My configuration looks like this: var webpack = require('webpack'); var fileNames = [ 'module1', //'module2', ]; function giveMeCon ...

Generate listview items containing anchor tags automatically

I am currently developing a web application using Jquery Mobile. After retrieving data from a webservice function, I am utilizing an ajax call to display this data on my webpage. $('[data-role=page]').on('pageshow', function () { var u ...

Avoiding cross-site cookie warnings when working with PostgreSQL through pgAdmin

Currently, my tech stack includes Python 3.7.4, Django 3.0.6, JavaScript, and Postgres 12.3.1. Upon loading my page, I am encountering the following warnings in the console: Cookie “PGADMIN_KEY” will be soon treated as cross-site cookie against “http ...

Tips for transforming a hover menu into a clickable menu

The scenario above demonstrates that when hovering over the dropdown menu, it displays a submenu. However, I want the submenu to be displayed after clicking on the dropdown text. Could anyone provide assistance on how to change the hovermenu to a clickabl ...

`year` method is not defined for DateTime parse

Having difficulty parsing a string to DateTime and encountering an error when attempting to exclude the year: undefined method `year' for "Monday, Aug 25, 10:30":String Controller dates = [] temps = [] dt = [] @data['data'].flatten.each ...

Can you explain the purpose of this variable "guess = require('myModule1')('myModule2');" ?

While examining a code snippet, I stumbled upon this line and am unsure which instance it is referring to. var guess = require('myModule1')('myMmodule2') ...

Learn the steps to Toggle Javascript on window resize

Is there a way to toggle Javascript on and off when the window is resized? Currently, resizing the window causes the navigation bar to stick and remain visible above the content. <script> if ( $(window).width() <= 1200 ) { }else{ $('nav& ...

Tips for managing JSON response

Here is the code I am using to process hit data in the URL. NSError *error; NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration de ...

Tips for ensuring that a nested object in an array contains only a single object

My array is structured like this: [ { object1:{ childObj1:[grandChild1,grandChild2], childObj1, childObj1} }, { object2:{ childObj1:[grandChild1,grandChild2], childObj1, childObj1} }, { object3:{ childObj1:[grandChild1,grandChild2 ...

Activate response upon verifying website link

I am adding functionality to my HTML page where I want certain sections to be visible when the user clicks on a link. Initially, these sections are hidden using the CSS property display: none;. My aim is to make these sections visible when the user clicks ...

Issue with modal component triggering unexpected page reload

I'm encountering a strange issue with my modal in Vue.js. It only appears on a specific page named 'Item', but when I click on a different view, the page reloads unexpectedly. This problem seems to occur only with the route containing the mo ...

Javascript parameter for Blogger URL

Looking to add additional URL parameters to my blog posts. I want to include ?id=100 or ?id=parameter at the end of the original link. For example, if someone visits my blog: www.example.com/?id=parameter example.blogspot.com/?id=parameter If someone op ...

Executing several Ajax requests at once can decrease the speed of the process

When I make simultaneous API calls using Ajax, the process seems to be slow as JavaScript waits for all API responses instead of fetching them asynchronously. For instance, /api/test1 usually responds in 5 seconds and /api/test2 also responds in 5 seconds ...

Having difficulty adding a marker to the map using JSON data

I am experiencing an issue where the marker is not appearing on the map. The JSON data seems to be correct as I can see it while debugging, but for some reason, the marker is not displaying on the map. public class MapsActivity extends FragmentActivity im ...

Is it necessary to add .html files to the public assets folder?

Currently, I am developing a web application using Node.js and Express. Typically, the views are stored separately from the public folder to account for dynamic content when utilizing Jade templating. However, if my .html files will always remain static, ...