JSON to C++ Struct Generator

Generate C++17 struct definitions from JSON with nlohmann/json serialization, std::optional for nullable fields, std::vector for arrays, and nested struct support.

Standard:

What is JSON to C++ Struct Generator?

This tool converts JSON data into C++ struct definitions with modern C++ types. It automatically infers C++ types from JSON values, converts field names to snake_case (C++ convention), handles nested structs, and generates ready-to-use C++ header code with optional nlohmann/json serialization support.

How to Use

  1. Set the root struct name and optional namespace
  2. Choose the C++ standard (C++11/14/17/20) — this affects nullable field types
  3. Optionally enable nlohmann/json serialization macros for automatic JSON conversion
  4. Optionally enable include guards for header file safety
  5. Paste your JSON data into the input area and click "Generate C++ Structs"

Why Use This Tool?

Automatically infer C++ types from JSON values
Convert field names to snake_case following C++ conventions
Generate nested structs with correct ordering (children first)
std::optional<T> for nullable fields (C++17+), std::unique_ptr<T> for C++11/14
std::vector<T> for arrays with automatic element type inference
Optional nlohmann/json NLOHMANN_DEFINE_TYPE_INTRUSIVE macros for seamless serialization

Tips & Best Practices

  • Use realistic sample data — type inference depends on actual values
  • C++17 is recommended for std::optional support; C++11/14 falls back to std::unique_ptr for nullable fields
  • The nlohmann/json mode adds NLOHMANN_DEFINE_TYPE_INTRUSIVE macros that enable automatic to_json/from_json conversion
  • Empty arrays default to std::vector<std::string> since element type cannot be inferred
  • Include guards use the pattern NAMESPACE_STRUCTNAME_HPP for header safety

Frequently Asked Questions

How are JSON types mapped to C++?

JSON strings → std::string, integers → int, decimals → double, booleans → bool, null → std::optional<T> (C++17+) or std::unique_ptr<T> (C++11/14), arrays → std::vector<T>, objects → separate nested structs.

What does the nlohmann/json option do?

When enabled, the tool includes <nlohmann/json.hpp> and adds NLOHMANN_DEFINE_TYPE_INTRUSIVE macros inside each struct. This provides automatic JSON serialization and deserialization, so you can use nlohmann::json::parse() and nlohmann::json(myStruct) directly.

Why use std::optional for null values?

std::optional<T> (C++17) explicitly represents a value that may or may not be present, which is the idiomatic C++ way to handle nullable fields. For C++11/14 projects, the tool falls back to std::unique_ptr<T> which serves a similar purpose.

How are arrays handled?

JSON arrays are converted to std::vector<T> where T is inferred from the first element. Arrays of objects become std::vector<NestedStruct>, arrays of strings become std::vector<std::string>, and so on. Empty arrays default to std::vector<std::string>.

Is my data sent to a server?

No, all processing happens entirely in your browser. Your JSON data never leaves your device.

Related Tools