Skip to content

Why is an object ordering values by Alphabetical Order?  #660

Closed

Description

@gDev95

Hi I am experiencing some weird behaviour I cannot really get behind why it does the following.
Imagine you want to have following json object:

{
	"result_parameters":{
		"Category": 1,
		"Username": "Warrior11",
		"First_Name": "Max",
		"Date_Signed_Up": [1,1,2017]
	}
}

What I did was:

nlohmann::json j;
j["result_parameter"]["Category"] = 1;
j["result_parameter"]["Username"] =  "Warrior11";
j["result_parameter"]["First_Name"] = "Max";
j["result_parameter"]["Date_Signed_Up"] = [1,1,2017];

When I print this ( cout << j.dump<< endl; ) I get following output:

{"result_parameter":{"Category":1,"Date_Signed_Up":[1,1,2017],"First_Name":"Max","Username":"Warrior11"}}

The order is messed up, how come?

Activity

  1. nlohmann commented on Jul 20, 2017

    @nlohmann
    Owner

    We do not preserve the insertion order of object elements. The JSON standard defines objects as "an unordered collection of zero or more name/value pairs". To this end, this library does not preserve insertion order of name/value pairs. (In fact, keys will be traversed in alphabetical order as std::map with std::less is used by default.) Note this behavior conforms to the standard, and we shall not change it to any other order. If you do want to preserve the insertion order, you can specialize the object type with containers like tsl::ordered_map or nlohmann::fifo_map.

  2. added
    solution: invalidthe issue is not related to the library
    solution: wontfixthe issue will not be fixed (either it is impossible or deemed out of scope)
    on Jul 20, 2017
  3. nlohmann commented on Jul 20, 2017

    @nlohmann
    Owner
  4. sqwunkly commented on Sep 6, 2017

    @sqwunkly

    Hi Niels,

    Thanks for your reply. I checked out #106 #424 & #660, unfortunately I'm not an very experienced programmer and I doubt I've the knowledge/expertise to get the fifo_map up and running myself.

    In #106 habemus-papadum tried to impliment the fifo_map but he seemed to run into significant problems and it appears he may not have resolved them, at least he didn't say so. He seems a lot more experienced than me.

    #424 and #660 also provide no guidance on how to implement the fifo_maps or another solution for outputting the json data in unsorted format.

    I'd appreciate if you could provide or point me to a working example for implementing the fifo_map using your json library, such that I can open existing json data, add objects, edit and delete and save the data in unsorted format in the same easy intuitive manner described in your examples, like this:-

    image

    Sorry if my request is of a basic level. If you could help I'd be much grateful, I feel others would also like to see a working example of how to implement the fifo_map to output unsorted json.

    Greg

  5. nlohmann commented on Sep 6, 2017

    @nlohmann
    Owner

    Using fifo_map and the like should be as simple as replacing the template parameter for the objects. Unfortunately, this seems not to work as I expected, and I am currently not aware of a working example.

  6. nlohmann commented on Sep 6, 2017

    @nlohmann
    Owner

    (In the future, I shall mark issues regarding object orders as "won't fix" rather than pointing to fifo_map...)

  7. nlohmann commented on Jul 11, 2020

    @nlohmann
    Owner

    To be fixed with #2258.

  8. self-assigned this
    on Jul 11, 2020
  9. added this to the Release 3.8.1 milestone on Jul 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

release item: ✨ new featuresolution: duplicatethe issue is a duplicate; refer to the linked issue insteadsolution: invalidthe issue is not related to the librarysolution: proposed fixa fix for the issue has been proposed and waits for confirmationsolution: wontfixthe issue will not be fixed (either it is impossible or deemed out of scope)

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions