Files
json/api/ordered_json.md
2026-08-04 07:02:38 +00:00

1.3 KiB

nlohmann::ordered_json

using ordered_json = basic_json<ordered_map>;

This type preserves the insertion order of object keys.

Iterator invalidation

The type is based on ordered_map which in turn uses a std::vector to store object elements. Therefore, adding object elements can yield a reallocation in which case all iterators (including the end() iterator) and all references to the elements are invalidated. Also, any iterator or reference after the insertion point will point to the same index, which is now a different value.

Complexity

ordered_map has no lookup index: every key-based object operation is a linear scan, so building or parsing an object of n keys costs O(n²) rather than O(n log n). See ordered_map complexity for the per-operation table and for measured numbers.

Examples

??? example

The example below demonstrates how `ordered_json` preserves the insertion order of object keys.

```cpp
--8<-- "examples/ordered_json.cpp"
```

Output:

```json
--8<-- "examples/ordered_json.output"
```

See also

Version history

Since version 3.9.0.