0

Working with a JSON data, javascript change the order of the objects from this:

{
    "01": { "disabled": 0, "factor": 10, "id": "01", "tickets": 0 },
    "02": { "disabled": 0, "factor": 10, "id": "02", "tickets": 0 },
    "03": { "disabled": 0, "factor": 10, "id": "03", "tickets": 0 },
    "04": { "disabled": 0, "factor": 10, "id": "04", "tickets": 0 },
    "05": { "disabled": 0, "factor": 10, "id": "05", "tickets": 0 },
    "06": { "disabled": 0, "factor": 10, "id": "06", "tickets": 0 },
    "07": { "disabled": 0, "factor": 10, "id": "07", "tickets": 0 },
    "08": { "disabled": 0, "factor": 10, "id": "08", "tickets": 0 },
    "09": { "disabled": 0, "factor": 10, "id": "09", "tickets": 0 },
    "10": { "disabled": 0, "factor": 10, "id": "10", "tickets": 0 },
    "11": { "disabled": 1, "factor": 40, "id": "11", "tickets": 0 }
}

To this:

{
    "10": { "disabled": 0, "factor": 10, "id": "10", "tickets": 0 },
    "11": { "disabled": 1, "factor": 40, "id": "11", "tickets": 0 },
    "01": { "disabled": 0, "factor": 10, "id": "01", "tickets": 0 },
    "02": { "disabled": 0, "factor": 10, "id": "02", "tickets": 0 },
    "03": { "disabled": 0, "factor": 10, "id": "03", "tickets": 0 },
    "04": { "disabled": 0, "factor": 10, "id": "04", "tickets": 0 },
    "05": { "disabled": 0, "factor": 10, "id": "05", "tickets": 0 },
    "06": { "disabled": 0, "factor": 10, "id": "06", "tickets": 0 },
    "07": { "disabled": 0, "factor": 10, "id": "07", "tickets": 0 },
    "08": { "disabled": 0, "factor": 10, "id": "08", "tickets": 0 },
    "09": { "disabled": 0, "factor": 10, "id": "09", "tickets": 0 }
}

Note the "10" and "11" rows at the begin even in the original JSON are at the end.

I fix it with this:

var data = JSON.parse(json_data);
var keys = Object.keys(data).sort();

for ( var k in keys )
{
    var key = keys[k];
    var row = data[key];
    console.log(row);
};

How to prevent javascript from changing the order of the rows?

That ERROR (because it is) made me mad. :-/

Thanks for any help.

junihh
  • 502
  • 1
  • 9
  • 25
  • 5
    Ordering of properties in JavaScript objects is not guaranteed. – Pointy Feb 23 '17 at 16:15
  • I knew it looking for an anwer. :-( – junihh Feb 23 '17 at 16:18
  • 1
    To elaborate, if you need to guarantee order, use an array. JSON isn't meant for human consumption, so expecting JavaScript to format it for human consumption won't end well. If you do need to format JSON for human reading, consider using an external utility like `jq`. It supports sorting by keys. – Mr. Llama Feb 23 '17 at 16:19
  • Use an array if the 'index' is important, or reuse the id property for it. If you're using this as a dictionary to directly reference records by id, I prefer using an array as the source and then just create the dictionary hash table on the run. – Shilly Feb 23 '17 at 16:20
  • what do you need to do with this data that makes the order important? – andi Feb 23 '17 at 16:27

1 Answers1

1

You should check bpierre's answer to "Does JavaScript Guarantee Object Property Order?".

To sum it up, the definition of an Object in ECMAScript says

It is an unordered collection of properties [...]

In his answer, he recommends the use of Map in the more recent versions of JavaScript.

So the answer is no, you can't prevent JavaScript from changing the order of the JSON, because there is no definitive order in the first place.

Community
  • 1
  • 1
Matheus Avellar
  • 1,507
  • 1
  • 22
  • 29