0

I have a post man request. Which returns a response data as:

[
  {
    "id": "7ca27c09-2b67-417e-b367-f97d49824a2f",
    "tags": [],
    "type": "ApplicationProfile",
    "userId": "2f2c8684-874d-49ea-bfa4-977069c1e3e2",
    "profile": {
      "User-Profile": {
        "PHI": {
          "gender": "Male",
          "surgeryDate": {
            "value": "2017-06-06"
          },
          "surgeryType": {
            "value": "gastricBand"
          },
          "heightInches": "72",
          "approvalPhase": "2",
          "profileImageUrl": "",
          "motivationalImageUrl": ""
        },
        "PII": {
          "mail": "c11@c111.com",
          "lastName": "",
          "firstName": "name1",
          "shouldReceiveNotifications": true
        }
      },
      "motivationalGoals": [
        {
          "goal": "Improving health",
          "goalId": "2957"
        },
        {
          "goal": "Relieving pain",
          "goalId": "2958"
        },
        {
          "goal": "Feeling better",
          "goalId": "2960"
        }
      ]
    },
    "version": 35,
    "createdBy": "2f2c8684-874d-49ea-bfa4-977069c1e3e2",
    "lastModifiedBy": "2f2c8684-874d-49ea-bfa4-977069c1e3e2",
    "applicationName": "Health Partner",
    "createdDateTime": "2017-06-08T16:08:32.497Z",
    "lastModifiedDateTime": "2017-06-09T13:23:03.503Z"
  },
  {
    "id": "091b5ebd-b096-436e-a703-f97f0ae77baf",
    "tags": [
      "Application-Profile"
    ],
    "type": "ApplicationProfile",
    "userId": "2f2c8684-874d-49ea-bfa4-977069c1e3e2",
    "profile": {
      "User-Profile": {
        "PHI": {
          "gender": "",
          "surgeryDate": {
            "value": "2017-06-01"
          },
          "surgeryType": {
            "value": ""
          },
          "heightInches": "72",
          "approvalPhase": "2",
          "profileImageUrl": "",
          "motivationalImageUrl": ""
        },
        "PII": {
          "mail": "c11@c111.com",
          "lastName": "Dev",
          "firstName": "Kat",
          "shouldReceiveNotifications": true
        }
      },
      "motivationalGoals": [
        {
          "goal": "Improving health",
          "goalId": "2957"
        },
        {
          "goal": "Relieving pain",
          "goalId": "2958"
        },
        {
          "goal": "Feeling better",
          "goalId": "2960"
        }
      ]
    },
    "version": 41,
    "createdBy": "2f2c8684-874d-49ea-bfa4-977069c1e3e2",
    "lastModifiedBy": "2f2c8684-874d-49ea-bfa4-977069c1e3e2",
    "applicationName": "Health Partner Weightloss",
    "createdDateTime": "2017-06-03T16:19:57.811Z",
    "lastModifiedDateTime": "2017-06-08T16:07:49.555Z"
  }
]

I need to extract the value of the emailid and set as global variable.

here is my code:

var jsonData1 = JSON.parse(responseBody);
postman.setGlobalVariable("jsonData1",jsonData1.profile.User-Profile.PII.mail);

But I am getting error. "User is undefined".

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • Does this answer your question? [How do I reference a javascript object property with a hyphen in it?](https://stackoverflow.com/questions/7122609/how-do-i-reference-a-javascript-object-property-with-a-hyphen-in-it) – Henke Jan 29 '21 at 10:56
  • This is a JavaScript issue rather than a Postman issue. – Henke Jan 29 '21 at 10:57

1 Answers1

0

The problem here is that the hyphen / minus sign is interpreted as the subtraction operator.

Consider changing your API and using "userProfile" instead of "User-Profile".

Valentin Despa
  • 40,712
  • 18
  • 80
  • 106