Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using $http to fetch a collection of users. The raw response from the server is this...

[{"id":2,"name":"John Doe","email":"[email protected]"}]

Logging the data parameter in the success callback shows this...

[Object, each: function, eachSlice: function, all: function, any: function, collect: function…]
  0: Object
    $$hashKey: "004"
    email: "[email protected]"
    id: 2
    name: "John Doe"
    __proto__: Object
  length: 1
__proto__: Array[0]

Good enough. Looks like $http already de-serialized the raw JSON into a JavaScript object.

Next, I assign the data to a $scope variable, inside the success callback, in order to perform some debugging in the browser...

$scope.debug = data;

Now, in my view, I want to display this as pretty JSON in order to debug.

<pre>{{debug | json}}</pre>

And I get this...

"[{\"id\": 2, \"name\": \"John Doe\", \"email\": \"[email protected]\", \"$$hashKey\": \"004\"}]"

I am trying to get something like this...

[
  {
    "id": 2,
    "name": "John Doe",
    "email": "[email protected]",
    "$$hashKey": "004"
  }
]

I also tried to stringify the javascript array in the controller...

$scope.debug = JSON.stringify(data, true);

and not use the filter...

<pre>{{debug}}</pre>

but I get the same results, except the $$hashKey has been removed...

"[{\"id\": 2, \"name\": \"John Doe\", \"email\": \"[email protected]\"}]"

If I just assign the first item in the array to $scope, and use the json filter, it works fine...

$scope.debug = data[0];

In my view...

<pre>{{debug | json}}</pre>

Results in...

{
  "id": 2,
  "name": "John Doe",
  "email": "[email protected]"
}

I know there are other ways to get what I want. I am just trying to understand what is going on.

Thanks!

share|improve this question
 
why don't you just use the console? O.o –  thescientist Oct 7 '13 at 15:29
 
Thanks. I am using the console, and that works fine. But, I would like to get to the bottom of the issue rather than just working around it. I want to understand why it does not work as I would expect. Either it is working correctly, and I misunderstand how it is supposed to work, or I understand correctly how it is supposed to work, but I am doing it slightly wrong, or I understand how it is supposed to work, and am doing it correctly, but there is some bug in the filter code. Just trying to learn. –  Kevin Oct 8 '13 at 19:03
add comment

1 Answer

You should parse the json instead of stringify.

Try this:

$scope.debug = JSON.parse(data)

Working Fiddle

share|improve this answer
 
Thanks Beteraraba, I tried that, but the issue is that within the success callback for the $http get call, the json has already been parsed into a JavaScript array object. –  Kevin Oct 9 '13 at 19:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.