0

I am trying to parse a value which is residing inside a json object . Below is the response from my API call.

var responseData = {
  "statusCode": 200,
  "body": "{\"Errors\":\"\",\"Message\":null,\"OrgID\":0,\"Output\":\"{\\\"AllUserDetails\\\":{\\\"UserDetails \\\":[{\\\"UserID\\\":1012,\\\"UserName\\\":\\\"Arul, Velu\\\",\\\"EmpID\\\":null,\\\"Role\\\":\\\"SA\\\",\\\"RoleName\\\":\\\"Super Admin\\\",\\\"EmailID\\\":\\\"[email protected]\\\",\\\"NT_UID\\\":\\\"ICS\\\\\\\\RNair\\\",\\\"Frm_UID\\\":\\\"[email protected]\\\",\\\"Frm_PWD\\\":\\\"3ZD1KS+4gEL5RKsjlC5k0MIfaiV8k\\/ixZRS6NpNyu0g=\\\",\\\"Org_Id\\\":1,\\\"Instance_Model\\\":\\\"Dependent\\\",\\\"Org_Name\\\":\\\"India\\\",\\\"AccessCreate_Portfolio\\\":false,\\\"AccessEdit_Portfolio\\\":false,\\\"Customer\\\":2592,\\\"Customer_Name\\\":\\\"ICS\\\",\\\"Location\\\":null,\\\"Location_Name\\\":\\\"\\\",\\\"Userlevel\\\":2544,\\\"User_Level\\\":\\\"Analyst\\\",\\\"Country\\\":null,\\\"Designation\\\":\\\"Consultant\\\",\\\"ContactNo\\\":\\\"2133232123\\\",\\\"MobileNo\\\":null,\\\"JoinDate\\\":\\\"2016-01-06 00:00:00\\\",\\\"Access_ServerMon\\\":false,\\\"Access_IncidentMgmt\\\":true,\\\"Access_CMCMDB\\\":true,\\\"Access_CMDB\\\":true,\\\"Access_RM\\\":true,\\\"Access_KnowledgeMgmt\\\":true,\\\"Access_PortfolioMgmt\\\":true,\\\"Access_ServiceRequest\\\":true,\\\"Access_ServiceCatalog\\\":true,\\\"Access_NetworkMon\\\":false,\\\"Access_AssetMgmt\\\":true,\\\"Access_ProblemMgmt\\\":true,\\\"Access_ProjectMgmt\\\":true,\\\"Access_SkillPack\\\":true,\\\"Access_FinMgmt\\\":false,\\\"Access_SLA\\\":true,\\\"SLAMgr\\\":false,\\\"AccessType_ServerMon\\\":\\\"No Access\\\",\\\"AccessType_IncidentMgmt\\\":\\\"No Access\\\",\\\"AccessType_CMCMDB\\\":\\\"No Access\\\",\\\"AccessType_CMDB\\\":\\\"\\\",\\\"AccessType_RM\\\":\\\"\\\",\\\"AccessType_KnowledgeMgmt\\\":\\\"\\\",\\\"AccessType_PortfolioMgmt\\\":\\\"\\\",\\\"AccessType_ServiceRequest\\\":\\\"\\\",\\\"AccessType_ServiceCatalog\\\":\\\"\\\",\\\"AccessType_NetworkMon\\\":\\\"No Access\\\",\\\"AccessType_AssetMgmt\\\":\\\"No Access\\\",\\\"AccessType_ProblemMgmt\\\":\\\"No Access\\\",\\\"AccessType_ProjectMgmt\\\":\\\"No Access\\\",\\\"AccessType_SkillPack\\\":\\\"No Access\\\",\\\"AccessType_WorkOrder\\\":\\\"No Access\\\",\\\"AccessType_FinMgmt\\\":\\\"No Access\\\",\\\"AccessType_SLA\\\":\\\"\\\",\\\"Address\\\":\\\"2901 Butterfield Rd\\\\n2nd Flooor\\\",\\\"UserRemarks\\\":\\\"\\\",\\\"State\\\":\\\"\\\",\\\"City\\\":\\\"\\\",\\\"Pin\\\":\\\"\\\",\\\"Active\\\":true,\\\"UT_ID\\\":90,\\\"AccessType_SkillPack1\\\":\\\"No Access\\\",\\\"Access_EProc\\\":true,\\\"AccessType_Eproc\\\":\\\"General Access\\\",\\\"Entity_Access\\\":\\\"\\\",\\\"TimeZone\\\":\\\"(UTC - 06:00) CST - Central Standard Time\\\",\\\"GMT_TimeDiff\\\":-300,\\\"Access_EventMgmt\\\":false,\\\"AccessType_EventMgmt\\\":\\\"No Access\\\",\\\"Keyword_Access\\\":\\\"\\\",\\\"ManagerName\\\":\\\"Arulvelu\\\",\\\"ProjMgmt_InstanceAccess\\\":\\\"\\\",\\\"Manager_UserID\\\":null,\\\"IM_ExtendedRole\\\":\\\"RPT\\\",\\\"AM_ExtendedRole\\\":\\\"RPT\\\",\\\"CustomerMasterID\\\":11,\\\"OrgLogoActive\\\":null,\\\"CustomerLogoActive\\\":null,\\\"AM_EditTemplateID\\\":0,\\\"DesignationID\\\":1381,\\\"PT_userAccessLevel\\\":\\\"Analyst\\\",\\\"PT_workgroupId\\\":0,\\\"PT_CostcentreID\\\":0,\\\"Alternative_EmailID\\\":\\\" , , ,\\\",\\\"ManagerEmailID\\\":null,\\\"ManagerEmployeeID\\\":null}]}}\",\"OutputObject\":null,\"TokenID\":null}",
  "headers": {
    "cache-control": "private",
    "content-type": "application/json; charset=utf-8",
    "server": "Microsoft-IIS/8.5",
    "set-cookie": [
      "ASP.NET_SessionId=2ncl0ezht5; path=/; HttpOnly"
    ],
    "x-aspnet-version": "4.0.30319",
    "x-powered-by": "ASP.NET",
    "date": "Sat, 13 Oct 2018 20:12:39 GMT",
    "connection": "close",
    "content-length": "3314"
  }
}

I could able to get the body value using responseData.body . But i need to retreive the value which is inside the body.Output.AllUserDetails.UserDetails [0].UserID It throws me an error stating AllUserDetailsis undefined. Can anyone assist me.

1
  • What are you using to make this "API call"? Most libraries (including the native fetch but excluding XMLHttpRequest) are able to automatically decode JSON response bodies Commented Oct 15, 2018 at 6:03

4 Answers 4

4

Try this:

JSON.parse(JSON.parse(responseData.body).Output).AllUserDetails['UserDetails '][0].UserID

Please note UserDetails has an extra space in api response so you can not access it using the dot notation.

Sign up to request clarification or add additional context in comments.

1 Comment

"Please note UserDetails has an extra space" 👈 now that's attention to detail. Well spotted!
1

The simple answer:

JSON.parse(JSON.parse(responseData.body).Output)

And then get whatever attribute you need.

The long answer:

the json inside your object is a string and not an object.

1 Comment

This is not really a long answer. ;)
0

You've to do multiple times JSON.parse then you can access user details

var responseData = {
  "statusCode": 200,
  "body": "{\"Errors\":\"\",\"Message\":null,\"OrgID\":0,\"Output\":\"{\\\"AllUserDetails\\\":{\\\"UserDetails \\\":[{\\\"UserID\\\":1012,\\\"UserName\\\":\\\"Arul, Velu\\\",\\\"EmpID\\\":null,\\\"Role\\\":\\\"SA\\\",\\\"RoleName\\\":\\\"Super Admin\\\",\\\"EmailID\\\":\\\"[email protected]\\\",\\\"NT_UID\\\":\\\"ICS\\\\\\\\RNair\\\",\\\"Frm_UID\\\":\\\"[email protected]\\\",\\\"Frm_PWD\\\":\\\"3ZD1KS+4gEL5RKsjlC5k0MIfaiV8k\\/ixZRS6NpNyu0g=\\\",\\\"Org_Id\\\":1,\\\"Instance_Model\\\":\\\"Dependent\\\",\\\"Org_Name\\\":\\\"India\\\",\\\"AccessCreate_Portfolio\\\":false,\\\"AccessEdit_Portfolio\\\":false,\\\"Customer\\\":2592,\\\"Customer_Name\\\":\\\"ICS\\\",\\\"Location\\\":null,\\\"Location_Name\\\":\\\"\\\",\\\"Userlevel\\\":2544,\\\"User_Level\\\":\\\"Analyst\\\",\\\"Country\\\":null,\\\"Designation\\\":\\\"Consultant\\\",\\\"ContactNo\\\":\\\"2133232123\\\",\\\"MobileNo\\\":null,\\\"JoinDate\\\":\\\"2016-01-06 00:00:00\\\",\\\"Access_ServerMon\\\":false,\\\"Access_IncidentMgmt\\\":true,\\\"Access_CMCMDB\\\":true,\\\"Access_CMDB\\\":true,\\\"Access_RM\\\":true,\\\"Access_KnowledgeMgmt\\\":true,\\\"Access_PortfolioMgmt\\\":true,\\\"Access_ServiceRequest\\\":true,\\\"Access_ServiceCatalog\\\":true,\\\"Access_NetworkMon\\\":false,\\\"Access_AssetMgmt\\\":true,\\\"Access_ProblemMgmt\\\":true,\\\"Access_ProjectMgmt\\\":true,\\\"Access_SkillPack\\\":true,\\\"Access_FinMgmt\\\":false,\\\"Access_SLA\\\":true,\\\"SLAMgr\\\":false,\\\"AccessType_ServerMon\\\":\\\"No Access\\\",\\\"AccessType_IncidentMgmt\\\":\\\"No Access\\\",\\\"AccessType_CMCMDB\\\":\\\"No Access\\\",\\\"AccessType_CMDB\\\":\\\"\\\",\\\"AccessType_RM\\\":\\\"\\\",\\\"AccessType_KnowledgeMgmt\\\":\\\"\\\",\\\"AccessType_PortfolioMgmt\\\":\\\"\\\",\\\"AccessType_ServiceRequest\\\":\\\"\\\",\\\"AccessType_ServiceCatalog\\\":\\\"\\\",\\\"AccessType_NetworkMon\\\":\\\"No Access\\\",\\\"AccessType_AssetMgmt\\\":\\\"No Access\\\",\\\"AccessType_ProblemMgmt\\\":\\\"No Access\\\",\\\"AccessType_ProjectMgmt\\\":\\\"No Access\\\",\\\"AccessType_SkillPack\\\":\\\"No Access\\\",\\\"AccessType_WorkOrder\\\":\\\"No Access\\\",\\\"AccessType_FinMgmt\\\":\\\"No Access\\\",\\\"AccessType_SLA\\\":\\\"\\\",\\\"Address\\\":\\\"2901 Butterfield Rd\\\\n2nd Flooor\\\",\\\"UserRemarks\\\":\\\"\\\",\\\"State\\\":\\\"\\\",\\\"City\\\":\\\"\\\",\\\"Pin\\\":\\\"\\\",\\\"Active\\\":true,\\\"UT_ID\\\":90,\\\"AccessType_SkillPack1\\\":\\\"No Access\\\",\\\"Access_EProc\\\":true,\\\"AccessType_Eproc\\\":\\\"General Access\\\",\\\"Entity_Access\\\":\\\"\\\",\\\"TimeZone\\\":\\\"(UTC - 06:00) CST - Central Standard Time\\\",\\\"GMT_TimeDiff\\\":-300,\\\"Access_EventMgmt\\\":false,\\\"AccessType_EventMgmt\\\":\\\"No Access\\\",\\\"Keyword_Access\\\":\\\"\\\",\\\"ManagerName\\\":\\\"Arulvelu\\\",\\\"ProjMgmt_InstanceAccess\\\":\\\"\\\",\\\"Manager_UserID\\\":null,\\\"IM_ExtendedRole\\\":\\\"RPT\\\",\\\"AM_ExtendedRole\\\":\\\"RPT\\\",\\\"CustomerMasterID\\\":11,\\\"OrgLogoActive\\\":null,\\\"CustomerLogoActive\\\":null,\\\"AM_EditTemplateID\\\":0,\\\"DesignationID\\\":1381,\\\"PT_userAccessLevel\\\":\\\"Analyst\\\",\\\"PT_workgroupId\\\":0,\\\"PT_CostcentreID\\\":0,\\\"Alternative_EmailID\\\":\\\" , , ,\\\",\\\"ManagerEmailID\\\":null,\\\"ManagerEmployeeID\\\":null}]}}\",\"OutputObject\":null,\"TokenID\":null}",
  "headers": {
    "cache-control": "private",
    "content-type": "application/json; charset=utf-8",
    "server": "Microsoft-IIS/8.5",
    "set-cookie": [
      "ASP.NET_SessionId=2ncl0ezht5; path=/; HttpOnly"
    ],
    "x-aspnet-version": "4.0.30319",
    "x-powered-by": "ASP.NET",
    "date": "Sat, 13 Oct 2018 20:12:39 GMT",
    "connection": "close",
    "content-length": "3314"
  }
}

var responseJSON = JSON.parse(JSON.stringify(responseData));
var responseJSONBody = JSON.parse(responseJSON.body)
var responseJSONBodyOutput = JSON.parse(responseJSONBody.Output)

console.log(responseJSONBodyOutput);

Comments

0

try this -

body.Output.AllUserDetails['UserDetails '][0].UserID

1 Comment

Are you sure about this? body is a string, and this should throw an error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.