Serialize a value as JSON
#nlohmann/json is a named third-party implementation; dump produces JSON text from its value model.
Fragment
Install/include nlohmann/json.hpp. Standard C++23 has no built-in JSON codec.
Reference
Choose TypeScript, Go, Rust or C++ and find syntax, everyday APIs, standard libraries and tools in one place.
TypeScript/Node, Go 1.23+, Rust 2024 and C++23: from a first CLI to files, JSON, HTTP, concurrency and testing. Complete programs are labeled; other examples identify their surrounding code/imports. Optional libraries and version requirements are named in each entry.
C++ docs std::basic_string ↗String literals ↗
nlohmann/json is a named third-party implementation; dump produces JSON text from its value model.
Fragment
Install/include nlohmann/json.hpp. Standard C++23 has no built-in JSON codec.
Check the parsed value’s shape and field type before extracting it. Library exceptions and domain errors need an owner recovery policy.
Fragment
Requires nlohmann/json.hpp, <string> and <stdexcept>. Choose a separate whitespace policy if blank-but-nonempty names are invalid.
find… returns a position or npos. substr takes a start and count, not an end index. size counts char units. back/pop_back require a nonempty string; clear empties it, push_back appends a character.
Fragment
Header plus independent fragments.
Single quotes form character literals; ordinary double-quoted literals are character arrays, not std::string objects. A raw literal preserves escapes. Since C++20, u8 literals use char8_t, so they are not ordinary char strings.
Fragment
Fragment: imports and type/function declarations at file scope; usage statements inside a function.
std::string is a sequence of char units. For a UTF-8 payload, size counts bytes; it does not decode Unicode or count grapheme clusters. Ordinary indexing can select one byte of a multibyte sequence.
Fragment
Fragment: imports and type/function declarations at file scope; usage statements inside a function.
std::string owns mutable text. +=/append extend it; replace(position, count, text) changes a selected range. string_view does not own or grow storage and can be invalidated by changes to its source.
Fragment
Fragment: imports and type/function declarations at file scope; usage statements inside a function.
The standard library has no built-in JSON parser. A named dependency such as nlohmann/json defines its own parsing and error contract.
Fragment
Requires the nlohmann/json dependency and an app-provided source string; this is not a standard-library API.