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 @danny-dainton

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.

@danny-dainton

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?

@danny-dainton

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.

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

What’s the syntax to dereference the json object properties from within an array, like this? I keep getting this: AssertionError: expected undefined to deeply equal ‘478-89-5114’

Using this test:

pm.test("t_SSNCheck", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData["ssn"]).to.eql("478-89-5114"); //{{eSSN}} = pm.environment.get("eSSN")
});
[
    {
        "orderId": 38590863,
        "ssn": "478-89-5114"
    }
]
1 Like

Hey @terand1967

It looks like you would need something like this:

pm.test("t_SSNCheck", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData[0].ssn).to.eql(pm.environment.get("eSSN"));
});
1 Like

Cool, thanks…that should work. Not enough coffee today… J

1 Like

Thank you! I had that issue too :slight_smile:

Any idea for issue:

In after response script i add:

let jsonData1 = pm.response.json();

In run time i receive Error:
TypeError: pm.response.json is not a function.

Also tried:
let jsonData2 = pm.response.messages.json();
Then jsonData2 is Undefined.

@spacecraft-operator8

This should probably be its own question.

This is a gRPC request\response and you don’t need to parse those , so pm.response.json() will error.

Please take a look at the following which has examples of working with the response elements.

Testing gRPC APIs with Postman | Postman Blog