You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.
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?
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);
Activity
gregmarr commentedon Jan 9, 2017
https://github.com/nlohmann/json/blob/master/.github/CONTRIBUTING.md
gregmarr commentedon Jan 9, 2017
#106
fyears commentedon Jan 12, 2017
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.
nlohmann commentedon Jan 12, 2017
The problem is that we are defining an object
basic_jsonand inside a member of typestd::map<std::string, basic_json>. At this point,basic_jsonis not yet fully defined. This seems to be OK forstd::mapon all platforms/compilers I encountered so far, but it is not forstd::unordered_map.code-blooded245 commentedon Jan 20, 2020
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?
nlohmann commentedon Jan 21, 2020
Can you give an example for this?
code-blooded245 commentedon Jan 22, 2020
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.
nlohmann commentedon Jan 25, 2020
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.
code-blooded245 commentedon Jan 27, 2020
Thanks for the support.
code-blooded245 commentedon Jan 28, 2020
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);
nlohmann commentedon Feb 15, 2020
You may be able to achieve this via the
items()function.nlohmann commentedon Jul 11, 2020
To be fixed with #2258.