Files
json/docs/mkdocs/docs/api/basic_json/flatten.md
T
Niels Lohmann 172eba6e46 docs: cross-link concept pages and API pages in See also sections
Add See also links in both directions between the concept pages (features,
home, integration) and the API pages, write API links in code font, give every
entry a comment after a dash, and show the callback event figure on the parser
callbacks page.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-26 16:17:03 +01:00

1.7 KiB

nlohmann::basic_json::flatten

basic_json flatten() const;

The function creates a JSON object whose keys are JSON pointers (see RFC 6901) and whose values are all primitive (see is_primitive() for more information). The original JSON value can be restored using the unflatten() function.

Return value

an object that maps JSON pointers to primitive values

Exception safety

Strong exception safety: if an exception occurs, the original value stays intact.

Complexity

Linear in the size of the JSON value.

Notes

Empty objects and arrays are flattened to #!json null and will not be reconstructed correctly by the unflatten() function.

Examples

??? example "Example: flatten a JSON object"

The following code shows how a JSON object is flattened to an object whose keys consist of JSON pointers.

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

Output:

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

??? example "Example: empty objects and arrays are flattened to #!json null"

The following code shows that an empty object and an empty array are both flattened to `#!json null`, and that
`unflatten()` restores them as `#!json null` rather than as empty containers.

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

Output:

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

See also

Version history

  • Added in version 2.0.0.