[JSON3] Order of the fields

Hi, is there a way to specify the order of the fields when writing with JSON3. E.g. in julia 1.7, JSON3.pretty(Dict("a" => 1, "b" => 2, "c" => 3, "d" => 4)) yields

{
   "c": 3,
   "b": 2,
   "a": 1,
   "d": 4
}

while the output I’m looking is:

{
   "a": 1,
   "b": 2,
   "c": 3,
   "d": 4
}

Just a guess, it prints what you want if you use an OrderedDict from DataStructures instead of the plain Dict?

(I think I’ve done that before… It was with TOML)

3 Likes

You could also use a NamedTuple, like JSON3.pretty((a=1, b=2, c=3, d=4))

1 Like

keep in mind JSON spec is order-less, while it might be pleasing to human eyes, make sure don’t depend on the order at any stage of your pipeline (yes, it has happened to me that someone wrote a order-dependent JSON parser in C++ and took 10 hours to debug)

1 Like