0

So, I have a similar situation as with this question which "SHOULD" work but doesn't

How can I access and process nested objects, arrays or JSON?

My JSON is basically this... reduced for brevity.

NOTE:

"{"message": [{"id":"1","element1":"Something","element1":"Something"},{"id":"2","element1":"Something","element1":"Something"}],"status": "success"}"

What happens is that if I select response coming from the API, I get the entire JSON.

If I type: response.message, I get UNDEFINED. That makes absolutely no sense.

enter image description here

This is what it looks like in sessionStorage:

enter image description here

I just want to do what I've always done: response.message or response.whatever to just give me what's "INSIDE" message and for some reason, no matter what I do, it's not working.

Here's the part of the code where I get the response with message

globalService.commonService('getData', reqPkg).then((response) => {
   $scope.theReport.data = JSON.parse(response);
});

if I do this I get undefined

$scope.theReport.data = JSON.parse(response.message);

I've tried JSON.stringify too, same result.

This is from a previous question which works fine and is in deed just the Objects from the Array of Objects.

const arrResult = Object.keys(objCount).map(k => ({'name': k, 'displayName': k, 'count': objCount[k] }));
Peter The Angular Dude
  • 1,112
  • 5
  • 26
  • 53
  • You should provide minimal working example so we can recreate your issue. Otherwise it sounds like a typo. – JustAskin Nov 24 '21 at 23:10
  • Do a `typeof response` and see if that returns `string` or `object`. If it is a `string` then you have a JSON string that must be parsed into an object via JSON.parse(response). If the type is 'object' then you have another issue. Important note! In strict using of the term, JSON refers to a string that can be de-seralized into an array/object, etc. JSON is used to move data around then it is transformed /into/ data. Alas, too many people refer to regular JavaScript objects as JSON because they look alike. (Go figure, JSON stands for JavaScript Object Notation.) – Jeremy J Starcher Nov 24 '21 at 23:10
  • 1
    I added more. I'm working for a Telecom Giant that is STILL using AngularJS. What's funny, in my previous call using MAP this isn't a problem. But there are over 3300+ rows coming back and it takes 21 seconds. That's why I store in sessionStorage after the first time – Peter The Angular Dude Nov 24 '21 at 23:13
  • 1
    `$scope.theReport.data = JSON.parse(response);` works? So now you have access to your object as `$scope.theReport.data.message[0].id` and so forth, no? – msanford Nov 24 '21 at 23:14
  • Correct but I don't want just one, I want ALL 3300 within MESSAGE – Peter The Angular Dude Nov 24 '21 at 23:15
  • 1
    `$scope.theReport.data = JSON.parse(response).message;` to get all entries from message into `$scope.theReport.data`. Your response is a string, so parse the response and just grabs the message key from that. – RenokK Nov 24 '21 at 23:17
  • RenokK trying now.... stand by – Peter The Angular Dude Nov 24 '21 at 23:19
  • 1
    Make it a solution and I'll vote for it. THANK YOU! That did it – Peter The Angular Dude Nov 24 '21 at 23:22

1 Answers1

1

$scope.theReport.data = JSON.parse(response).message; to get all entries from message into $scope.theReport.data. Your response is a string, so parse the response and just grab the message key from that.

RenokK
  • 700
  • 8
  • 24