Search code examples
postmanpostman-testcase

Parsing nested, "named" objects in Postman


I am trying to parse JSON data returned in a Postman test for the first time but the complexity of my payload is causing parsing issues: how can I get to the "stage-guid" field in

{
    "Workflow": {
        "com.mycom.MyWorkflow": {
            "id": 10,
            "stages": [
                {
                    "com.mycom.MyStage": {
                        "id": 22,
                        "stage-guid": "00000000-0000-1111-1111-123456789012",

The following gets me to the Workflow object, but I have not figured out how to access inside the com.mycom.MyWorkflow object.

        var responseInJson = pm.response.json(); 
        var workFlowData = responseInJson.Workflow;
        console.log("in workflow", workFlowData);

I have tried workflowData.id, workflowData.object.id, workflowData.Object.id, workflowData[pm.variables.get("stage-guid")] but the test case never proceeds beyond these (incorrect) access attempts.


Solution

  • You would need to walk down the structure to get the value that you need. As there are objects and arrays to navigate, you would need to reference these correctly.

    You also have property names with dots and dashes in them so you would need to use a mixture of dot and bracket notation to correctly reference those.

    This should log that guide value in the console:

    let responseInJson = pm.response.json(),
        workFlowData = responseInJson.Workflow["com.mycom.MyWorkflow"].stages[0]["com.mycom.MyStage"]["stage-guid"];
    
    console.log(workFlowData);