JSON to C++ Struct Generator
Generate C++ struct definitions from JSON objects. Produces structs with nlohmann/json from_json and to_json function macros, mapping JSON types to appropriate C++ types including std::string, int64_t, double, bool, and std::vector.
Generates C++ structs with nlohmann/json macro bindings.
Maps JSON types to C++ standard types (std::string, int64_t, double, bool, std::vector<T>).
Produces NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE macros for clean JSON binding.
Creates nested struct definitions for nested JSON objects.
Compatible with the popular nlohmann/json single-header library.
How to Use
Paste a JSON object into the input field
The generator produces C++ struct definitions with nlohmann/json macros
Include nlohmann/json.hpp in your project
Use nlohmann::json::parse and get<T>() to deserialize your structs
C++ Applications
- Parse REST API responses in C++ clients
- Read JSON configuration files in C++ services
- Deserialize JSON messages in embedded systems
Game Development
- Parse game config JSON files
- Load level data from JSON assets
- Deserialize network payloads in game servers
C++ Environments
- CMake Projects
- Game Engines
- Embedded Linux Services
- High-Performance Backends
For optional JSON fields, use std::optional<T> (C++17) combined with a custom from_json function instead of the macro.
Include nlohmann/json as a single-header dependency or via CMake FetchContent — it is one of the most popular C++ libraries.
JSON numbers without decimals become int64_t for maximum range; use int32_t manually if you're certain values fit and memory is constrained.
Use std::optional<T> (C++17) for JSON fields that can be null or absent to avoid undefined behavior when the key is missing.
Prefer int64_t over int for JSON integer fields to safely handle large identifier values and timestamps.
Add nlohmann/json via CMake FetchContent or vcpkg rather than manually copying the header to keep your dependency management clean.
Frequently Asked Questions
Find answers to common questions about our tools and services.
Understanding JSON to C++ Struct
C++ is widely used in domains where performance, low latency, and direct hardware access are critical — game engines, real-time systems, high-frequency trading, robotics, embedded Linux services, and scientific computing. These systems increasingly communicate with REST APIs, microservices, and configuration systems that use JSON as their data format. While C++ was historically associated with binary protocols and custom serialization formats, the modern C++ ecosystem has embraced JSON through the nlohmann/json library, which has become one of the most starred C++ projects on GitHub. Its single-header distribution model and intuitive API make it the go-to choice for JSON handling in C++ projects that do not have strict performance budgets for JSON parsing.
The nlohmann/json library offers multiple approaches to JSON deserialization into C++ types. For simple structured data, the NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE macro is the most convenient approach: it accepts a struct type name and a list of field names, then automatically generates from_json and to_json functions that map JSON keys to struct fields by name. This macro-based approach requires no modification to the struct definition itself, making it non-intrusive and compatible with structs defined in header files shared with code that does not use nlohmann/json. The generated binding is type-checked at compile time through template specialization, catching type mismatches between the JSON and the C++ struct at build time rather than at runtime.
C++'s type system requires careful mapping from JSON's dynamically typed values to statically typed C++ fields. JSON strings become std::string. JSON integers become int64_t for maximum range portability across 32-bit and 64-bit platforms, avoiding overflow risks that would occur with int or int32_t for large API identifiers or timestamps. JSON floating-point numbers become double. JSON booleans become bool. JSON arrays become std::vector<T> with the appropriate element type. JSON null is the most complex case: C++ has no built-in null reference semantics for value types, so nullable JSON fields require std::optional<T> (available since C++17) to represent the present/absent distinction correctly.
This JSON to C++ Struct generator produces struct definitions with NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE macro bindings, standard C++ field types following the mapping conventions above, and nested struct definitions for nested JSON objects. The output is compatible with modern C++ standards (C++11 and above for basic features, C++17 for std::optional). Adding the nlohmann/json single-header file or including it via CMake's FetchContent command is all that is needed to compile and run the generated code. For C++ developers building API clients, game data loaders, configuration parsers, or network message deserializers, this generator provides a significant head start on the data modeling work required for any JSON integration.