Coder Perfect

What does the $$hashKey in my JSON.stringify output mean?

Problem

I looked on Mozilla’s JSON stringify page in their docs, as well as here on SO and Google, but couldn’t find anything. I’ve used JSON.stringify a lot, but this is the first time I’ve seen this result.

I have a collection of JSON objects:

[
    {
        "param_2": "Description 1",
        "param_0": "Name 1",
        "param_1": "VERSION 1"
    },
    {
        "param_2": "Description 2",
        "param_0": "Name 2",
        "param_1": "VERSION 2"
    },
    {
        "param_2": "Description 3",
        "param_0": "Name 3",
        "param_1": "VERSION 3"
    }
]

lowing:

   [
        {
            "param_2": "Description 1",
            "param_0": "Name 1",
            "param_1": "VERSION 1",
            "$$hashKey": "005"
        },
        {
            "param_2": "Description 2",
            "param_0": "Name 2",
            "param_1": "VERSION 2",
            "$$hashKey": "006"
        },
        {
            "param_2": "Description 3",
            "param_0": "Name 3",
            "param_1": "VERSION 3",
            "$$hashKey": "007"
        }
    ]

I’m just interested about the $$hashkey property, since I expected something more along the lines of this from the stringify method (i.e., without the $$hashkey):

[
    {
        "1":{
            "param_2": "Description 1",
            "param_0": "Name 1",
            "param_1": "VERSION 1"
        },
         "2":{
            "param_2": "Description 2",
            "param_0": "Name 2",
            "param_1": "VERSION 2"
        },
         "3":{
            "param_2": "Description 3",
            "param_0": "Name 3",
            "param_1": "VERSION 3"
        }
    }
]

I’m not sure if it’s a factor, but here’s what I’m using:

On the server side, I’m also using Spring security 3.0.7.

I’m not having any problems with it, but I’d like to know what the $$hashkey is for.

Asked by jonnie

Solution #1

Angular uses this to keep track of your modifications and determine when the DOM needs to be updated.

Angular will strip out these internal-use values for you if you use angular.toJson(obj) instead of JSON.stringify(obj).

Angular will not need to add $$hashKey if you update your repeat expression to use the track by uniqueProperty suffix. As an example,

<ul>
    <li ng-repeat="link in navLinks track by link.href">
        <a ng-href="link.href">{{link.title}}</a>
    </li>
</ul>

Just keep in mind that the “link.” element of the expression is required – something I frequently overlook. Tracking by URL alone will almost certainly fail.

Answered by David Boike

Solution #2

The recommended way in my use case (feeding the generated object to X2JS)

data = angular.toJson(source);

help to remove the $$hashKey attributes, but X2JS would no longer be able to process the result.

data = angular.copy(source);

The $$hashKey attributes were also removed, although the output was still useable as an X2JS parameter.

Answered by rob2universe

Solution #3

It commonly comes with the ng-repeat directive. AngularJS uses a specific id to mark objects for dom manipulation.

This is a typical Angular problem. For example, if you get an object with ngResource, your object will include all of the resource API, including $save and other functions. AngularJS will also add a __ngDebug property to cookies.

Answered by Thomas Pons

Solution #4

If you don’t want to add ids to your data, you can track by the array’s index, which means the items will be indexed by their position in the array rather than their value.

Like this:

var myArray = [1,1,1,1,1];

<li ng-repeat="item in myArray track by $index">

Answered by Michael Falck WedelgÄrd

Solution #5

I propose using “track by” in your ng-repeat if you’re using Angular 1.3 or higher. If you use “track by,” Angular does not add a “$$hashKey” property to the objects in your array. If something in your array changes, angular doesn’t recreate the full DOM structure for your ng-repeat; instead, it recreates the area of the DOM that contains the altered values in your array.

Answered by Ajay Ullal

Post is based on https://stackoverflow.com/questions/18826320/what-is-the-hashkey-added-to-my-json-stringify-result