Advertisement

JSON to C++ Struct

Convert JSON into C++ structs using STL types. Get std::string, std::vector, std::optional, and nested structs with the needed includes.

JSON input

Paste JSON, optionally name the root type, then generate.

Your C++ struct code will appear here.
Advertisement

STL native data shapes

The generator maps JSON types onto the standard library: strings to std::string, numbers to int or double, booleans to bool, arrays to std::vector, and uncertain fields to std::optional. The required #include lines are emitted at the top.

Pair with nlohmann/json

With nlohmann/json, deserialization is nearly automatic: define the structs, then call j.get(). Combined with std::optional, missing keys map cleanly instead of throwing.

JSON to C++ Struct FAQs

Which JSON library pairs with this output?
The structs are designed for nlohmann/json, the most popular C++ JSON library. Its automatic from_json mapping fills std::string, std::vector, and std::optional fields directly.
Why std::optional for some fields?
Fields that are null or missing in the sample become std::optional<T>, which is the idiomatic C++17 way to represent values that may not exist.
How are arrays represented?
JSON arrays become std::vector<T> with T inferred from the elements, including vectors of nested structs for arrays of objects.
What C++ standard is required?
std::optional needs C++17. If you are stuck on C++14, replace std::optional<T> fields with a bool flag or a sentinel value.
How are nested objects handled?
Each nested object becomes its own struct declared before the parent, so the whole hierarchy compiles in dependency order with no forward declarations needed.
Advertisement