Skip to tool

JSON to C++ Structs

JSON to C++ Structs tool on AzWebTools.

Result

Fill inputs and click run.

How to Use This Tool

  1. Paste your valid JSON payload into the main input editor.
  2. Enter your desired C++ Root Struct Name (e.g., 'ApiResponse') and an optional Namespace (e.g., 'Network::Models').
  3. Use the dropdown menu to choose whether to generate nlohmann/json serialization functions or plain C++ structs.
  4. Click 'Generate C++ Code' to process the payload.
  5. Review the generated C++ header code and copy it to your clipboard for immediate use in your project.

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 int or int64_t, while floating-point numbers are mapped to double.
  • 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++

C++ is a highly performant, statically typed language. Because it predates the web era, C++ has never included a native JSON parser in its Standard Template Library (STL). As REST APIs and JSON became the global standard for data interchange, the C++ community developed robust third-party libraries. The most prominent today is Niels Lohmann's 'JSON for Modern C++' (released in 2013), which revolutionized JSON handling in C++ by utilizing C++11 features to create an intuitive, STL-like API.
Modern C++ relies on community libraries like nlohmann/json to bridge the gap between dynamically typed JSON payloads and statically typed C++ structs.
Language Designer
Bjarne Stroustrup
Standard Library JSON
None (Requires third-party headers)
Popular Serialization
nlohmann/json (C++11+)

Examples

Standard Structs (Types Only)

Runtime-verified example for json-to-c-structs
Input
{"rootName":"GameData","namespaceName":"core","generationMode":"Types Only"}
Output
{
  "rootName": "GameData",
  "namespaceName": "core",
  "generationMode": "Types Only"
}

With Nlohmann Serialization

Runtime-verified example for json-to-c-structs
Input
{"rootName":"ApiResponse","namespaceName":"network","generationMode":"Include nlohmann/json parsers"}
Output
{
  "rootName": "ApiResponse",
  "namespaceName": "network",
  "generationMode": "Include nlohmann/json parsers"
}

Sample Scenario

Runtime-verified example for json-to-c-structs
Input
{"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"}
Output
{
  "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.

Frequently Asked Questions