- Paste your valid JSON payload into the main input editor.
- Enter your desired C++ Root Struct Name (e.g., 'ApiResponse') and an optional Namespace (e.g., 'Network::Models').
- Use the dropdown menu to choose whether to generate
nlohmann/jsonserialization functions or plain C++ structs. - Click 'Generate C++ Code' to process the payload.
- Review the generated C++ header code and copy it to your clipboard for immediate use in your project.
JSON to C++ Structs
JSON to C++ Structs tool on AzWebTools.
Result
Fill inputs and click run.
How to Use This Tool
Learn More About JSON to C++ Structs
Understanding JSON to C++ Type Mapping
When converting JavaScript Object Notation (JSON) to C++, dynamic types must be mapped to C++'s statically typed system. Because C++ requires explicit type declarations, generators must infer the safest generic type for each JSON field.
- Strings: Mapped directly to
std::string. - Numbers: Integers are mapped to
intorint64_t, while floating-point numbers are mapped todouble. - Booleans: Mapped to
bool. - Arrays: Mapped to
std::vector<T>, which requires the#include <vector>header. - Nested Objects: C++ does not natively support anonymous nested structs intuitively when paired with external serialization libraries. Therefore, nested JSON objects are automatically extracted into their own named C++ structs and referenced by the parent struct.
The `nlohmann/json` Library
Modern C++ does not have a standard library implementation for JSON. The most popular community-driven solution is nlohmann/json, a header-only library for C++11 and later. It allows developers to treat JSON objects like first-class C++ STL containers. By defining `tojson` and `fromjson functions (or using the NLOHMANNDEFINETYPENONINTRUSIVE` macro) in the same namespace as your structs, the library automatically serializes and deserializes your C++ objects without manual mapping. This generator optionally creates these mapping functions to save you from writing repetitive parsing code.
Working with JSON in Modern C++
- Language Designer
- Bjarne Stroustrup
- Standard Library JSON
- None (Requires third-party headers)
- Popular Serialization
- nlohmann/json (C++11+)
Examples
Standard Structs (Types Only)
{"rootName":"GameData","namespaceName":"core","generationMode":"Types Only"}{
"rootName": "GameData",
"namespaceName": "core",
"generationMode": "Types Only"
}With Nlohmann Serialization
{"rootName":"ApiResponse","namespaceName":"network","generationMode":"Include nlohmann/json parsers"}{
"rootName": "ApiResponse",
"namespaceName": "network",
"generationMode": "Include nlohmann/json parsers"
}Sample Scenario
{"jsonPayload":"{\n \"player\": {\n \"id\": 104,\n \"username\": \"Hero\",\n \"position\": [0.0, 15.5, -3.2]\n },\n \"active\": true\n}","rootName":"GameState","namespaceName":"game_logic","generationMode":"Types Only"}{
"jsonPayload": "{\n \"player\": {\n \"id\": 104,\n \"username\": \"Hero\",\n \"position\": [0.0, 15.5, -3.2]\n },\n \"active\": true\n}",
"rootName": "GameState",
"namespaceName": "game_logic",
"generationMode": "Types Only"
}Use Cases
- Building C++ clients for REST APIs by automatically generating data models for JSON responses.
- Parsing game configuration files, save states, or level data stored in JSON format.
- Bootstrapping backend C++ server applications that communicate over WebSockets with JSON payloads.
- Migrating dynamically typed JSON data into strongly typed C++ architectures with minimal manual typing.