Pm.response.json() - Response as Object

Hi Experts,

I want to bring dynamicity when parsing through the API response received. To achieve that, trying to pass the Key to the object and get the value, which is achievable with javascript.

My response body:

{
   "name":{
      "title":"string",
      "forename":"string",
      "mid_initial":"string",
      "surname":"string",
      "suffix":"string"
   }
}

Theoretically, I should be able to get “John” with

var jsonData = pm.response.json();

var x = jsonData["name.title"];

however, this is not happening as expected; can someone give me an expertise suggestion.

Maybe I am not parsing the response body; how can I get response body alone from pm.response

Hey @srk.groups,

Welcome to the community! :rocket:

It looks like you using both bracket and dot notion in the same line.

You should be able to use either of these to get the value from the object.

var x = jsonData["name"]["title"];

var x = jsonData.name.title;   

However, if your response was something like this:

{
   "name.title":"string"
}

Then your syntax would be correct as that would be the correct way to access that keys value.

Hope that helps. :slight_smile:

Thank you for the comments @dannydainton

was trying to do something like this

pm.expect(jsonData[“name.title”]).to.eql(pm.iterationData.get(“name.title”))

Is there some sort of possibility; as “name.title” will be passed as a header from csv.

That should work fine with a CSV data file in the runner.

pm.test("Basic Test", function () {
    pm.expect(jsonData.name.title).to.equal(pm.iterationData.get("name.title"));
});

If the column header is name.title is will pick up that reference and use the value from that in the assertion.

@dannydainton

yes that will work; the ultimate goal is to iterate multiple columns from csv - i am still in research mode.

For that part it the solution will not work:-pm.expect(jsonData.name.title).to.equal(pm.iterationData.get(“name.title”)

say,
csv holds:
name.title,name.firstname,name.lastname
mr.,someone,somewhere

in this i am trying to bring in the dynamicity; :slight_smile:

2 things here i want o achieve,

  1. Iterate through column of csv,
  2. Set object property dynamically

Example:
1st iteration of the loop
pm.expect(jsonData[“ name.title ”]).to.eql(pm.iterationData.get(“ name.title ”))

2nd iteration of the loop
pm.expect(jsonData[“ name.firstname ”]).to.eql(pm.iterationData.get(“ name.firstname ”))

3rd iteration of the loop
pm.expect(jsonData[“ name.lastname ”]).to.eql(pm.iterationData.get(“ name.lastname ”))

if your can provide me some direction, that would be a help

Can you add some images or what you have so far, just so I can understand how far you’ve got and what’s not working?

If I understand correctly, is this what you’re trying to do?

I’ve just set your response data as a variable inside the Tests script but it would work the same if you use pm.response.json(). Each request in the collection has a single test, to compare the response data with the data in the CSV file.

name.title,name.firstname,name.lastname
mr,someone,somewhere

If this isn’t what you were trying to do, could you share the structure of your collection, please?

@dannydainton

Commented part is a prototyping of what i want to achieve; sample csv also attached the “DrillDownToResponse” is a boolean operator which pivots whether i need to drill down into response details or not.

@dannydainton and Team - can you give me some direction on this.