Skip to content

Automatic ordered JSON #424

Description

@MilowB

Hi,

Json is automatically sorted by an alphabetic order. I don't find any information about that in your readme. Is there a way to avoid sorting ?

Activity

  1. gregmarr commented on Jan 9, 2017

    @gregmarr
    Contributor

    https://github.com/nlohmann/json/blob/master/.github/CONTRIBUTING.md

    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 it to any other order.

  2. gregmarr commented on Jan 9, 2017

    @gregmarr
    Contributor

    #106

    You can check out https://github.com/nlohmann/fifo_map - it should work as a replacement of std::map and maintains the key's order.

  3. fyears commented on Jan 12, 2017

    @fyears

    Hi. A quick question regarding this issue (just out of curious): why not use std::unordered_map by default instead of std::map? After all, json objects are unsorted by semantics. And unordered_map should gain significant speed up regarding the time complexity.

  4. nlohmann commented on Jan 12, 2017

    @nlohmann
    Owner

    The problem is that we are defining an object basic_json and inside a member of type std::map<std::string, basic_json>. At this point, basic_json is not yet fully defined. This seems to be OK for std::map on all platforms/compilers I encountered so far, but it is not for std::unordered_map.

  5. code-blooded245 commented on Jan 20, 2020

    @code-blooded245

    Hi, is there any way to sort nlohmann::json depending upon values, like for example,
    if key is string and value is double, can I sort json based on double?

  6. nlohmann commented on Jan 21, 2020

    @nlohmann
    Owner

    Can you give an example for this?

  7. code-blooded245 commented on Jan 22, 2020

    @code-blooded245

    Example:
    if input = {
    "abc" : 0.288,
    "pqr" : 0.176,
    "xyz" : 0.555,
    "zaz" : 0.345
    }
    After sorting it should look something like,
    output = {
    "pqr" : 0.176,
    "abc" : 0.288,
    "lmo" : 0.345,
    "xyz" : 0.555
    }
    Basically sorted by values.

  8. nlohmann commented on Jan 25, 2020

    @nlohmann
    Owner

    This is not possible in the library (see https://github.com/nlohmann/json#order-of-object-keys). You could parse the value into some C++ container (e.g., a vector of pairs) and sort it yourself.

  9. code-blooded245 commented on Jan 27, 2020

    @code-blooded245

    Thanks for the support.

  10. code-blooded245 commented on Jan 28, 2020

    @code-blooded245

    Hey, is there a way to use std::max_element with nlohmann::json to get the key-value pair with the highest value for the above-given example?
    prototype:
    auto max = std::max_element (j.begin(), j.end(), cmp);

  11. nlohmann commented on Feb 15, 2020

    @nlohmann
    Owner

    You may be able to achieve this via the items() function.

  12. nlohmann commented on Jul 11, 2020

    @nlohmann
    Owner

    To be fixed with #2258.

  13. self-assigned this
    on Jul 11, 2020
  14. 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

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions