1
var json = `{"3":0,"2":0,"1":0}`
var obj = JSON.parse(json)
console.log(JSON.stringify(obj))
console.log(json === JSON.stringify(obj))

output

{"1":0,"2":0,"3":0}
false

I expect to get it

{"3":0,"2":0,"1":0}
true

How to do

xausky
  • 31
  • 6
  • 1
    https://stackoverflow.com/questions/5467129/sort-javascript-object-by-key – NiVeR Mar 12 '19 at 14:53
  • in this case you can simply print them backwards – messerbill Mar 12 '19 at 14:55
  • how do you access the keys later? – Nina Scholz Mar 12 '19 at 14:57
  • "Talk is cheap, show me the code.", Stackoverflow is not a MacDonald drive-through. –  Mar 13 '19 at 12:13
  • I would do this: `json = "{\"3\":0,\"2\":0,\"1\":0}"; obj = JSON.parse(json); json = JSON.stringify(obj); console.log(json === JSON.stringify(obj));`. But there is still no guarantee that it will *always* be true. You should write your own "stringify" function. –  Mar 13 '19 at 12:25

4 Answers4

4

This is because json is an object after parsing and object's keys are not guaranteed to be in order

You can use map & it guarantees the order of the keys

brk
  • 48,835
  • 10
  • 56
  • 78
  • It is not correct. object keys also guaranteed order, but keys as index is order by alphabet , other keys can keep insertion order.https://262.ecma-international.org/11.0/#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys – yang zhou Feb 20 '21 at 02:16
2

That's not possible. In ES6, the keys are traversed in the following order:

  • First, the keys that are integer indices, in ascending numeric order.

  • Then, all other string keys, in the order in which they were added to the object.

  • Lastly, all symbol keys, in the order in which they were added to the object.

The integer keys are sorted in ascending order irrespective of the order in which they are inserted

var a = {};
a[3] = "three"
a[2] = "two"
a[1] = "one"

console.log(JSON.stringify(a))

Reference

You might also want to read: Does JavaScript Guarantee Object Property Order?

Community
  • 1
  • 1
adiga
  • 34,372
  • 9
  • 61
  • 83
  • you could at least write a method which handles this stuff, but yes, its not possible just by using `JSON.parse()` and `JSON.stringify()` – messerbill Mar 12 '19 at 14:58
  • @messerbill Even if you add each key manually in descending order, `JSON.stringify(obj)` will always have `1` in the beginning – adiga Mar 12 '19 at 15:03
  • yes of course, but like i said - you could write a custom method using `map` for example – messerbill Mar 12 '19 at 15:28
0

You can use array of objects with guid property

const text = '[{"id":3,"value":0},{"id":2,"value":0},{"id":1,"value":0}]'

const json = JSON.parse(text)

console.log(JSON.stringify(json))
AlexOwl
  • 869
  • 5
  • 11
0

You could spend the keys a dot and by iteratong these keys are in the original insertation order, but not accessable with just the number. This needs a dot as well.

var json = `{"3.":0,"2.":0,"1.":0}`
var obj = JSON.parse(json)

console.log(obj[2])
console.log(JSON.stringify(obj))
console.log(json === JSON.stringify(obj))
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392