0

This might be a silly question but I am quite new using json, and I do not fully understand this:

(Using this online compiler)

import json
print("Hello World")
print(json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':')))

As far as I've understood: json.dumps takes an object and produces a string

And the output seems right:

[1,2,3,{"6":7,"4":5}]

I searched for a way to make it more readable, and there is a parameter called indentthat does the job well. But I do not understand why the output is different.

print(json.dumps([1,2,3,{'4': 5, '6': 7}], indent=2, separators=(',',':')))

Output:

[                                                                                                                              
  1,                                                                                                                           
  2,                                                                                                                           
  3,                                                                                                                           
  {                                                                                                                            
    "6":7,                                                                                                                     
    "4":5                                                                                                                      
  }                                                                                                                            
] 

Shouldn't it be first "4":5, "6":7and not the other way around?

(Expected result?)

  [                                                                                                                              
      1,                                                                                                                           
      2,                                                                                                                           
      3,                                                                                                                           
      {                                                                                                                            
        "4":5,                                                                                                                     
        "6":7                                                                                                                      
      }                                                                                                                            
    ] 

Thanks in advance!

Louis Storming
  • 151
  • 1
  • 10
  • `Yes, the order of elements in JSON arrays is preserved`, but if I understood it correctly (I might be wrong, quite new to json), the order here is not preserved! @MikeScotty – Louis Storming Dec 17 '18 at 09:42
  • 1
    The order of elements in an array ([]) is maintained. The order of elements (name:value pairs) in an "object" ({}) is not. – Stef van der Zon Dec 17 '18 at 09:43
  • 1
    The order of object keys in JSON / JS is not preserved (nor is it important), which is also answered in the linked question. Neither is the order of dictionary keys in python prior to 3.6 (where it first became an implementation detail). Optionally, you could use ``sort_keys`` in your [dumps](https://docs.python.org/3.6/library/json.html#json.dumps), which will sort the keys alphabetically in the output, but still the order will not be preserved. – Mike Scotty Dec 17 '18 at 09:45
  • Okay thank you! Should I delete de question? Or mark it as a duplicated? @MikeScotty – Louis Storming Dec 17 '18 at 09:49

0 Answers0