FormatterHQ

JSON to C++ Class Generator

Generate C++ classes from JSON with getters, setters and std::optional for missing values, plus from_json / to_json functions for the nlohmann/json library.

Input JSON
Output CPP
Ready.

How to use the JSON to C++

  1. Paste your JSON sample (an object, or an array of objects) into the Input editor, or use Sample, Upload file or Load URL, or press Sample to try one.
  2. Set the Top-level class name, for example Task; it is converted to PascalCase. Nested types are named after their property keys.
  3. Leave Types only (no helpers) off to include the nlohmann/json from_json / to_json code; tick it for the classes alone.
  4. Press Generate (it also re-runs automatically as you edit), then use Copy or Download.

JSON to C++: classes for nlohmann/json

C++ has no built-in JSON support, and the most popular library for it is nlohmann/json, a single-header library that converts between JSON and user types through from_json and to_json functions. This JSON to C++ generator produces both: classes that mirror your sample, and the conversion functions that let you write Task t = nlohmann::json::parse(text);.

Example

{
  "id": 42,
  "title": "Write docs",
  "done": false,
  "priority": 2.5,
  "tags": ["docs"],
  "owner": { "name": "Ada", "email": null },
  "due": "2026-10-01T09:00:00Z",
  "subtasks": [
    { "title": "Outline", "estimate": 2 },
    { "title": "Draft" }
  ]
}

The Subtask class, as generated:

class Subtask {
    public:
    Subtask() = default;
    virtual ~Subtask() = default;

    private:
    std::string title;
    std::optional<int64_t> estimate;

    public:
    const std::string & get_title() const { return title; }
    std::string & get_mutable_title() { return title; }
    void set_title(const std::string & value) { this->title = value; }

    const std::optional<int64_t> & get_estimate() const { return estimate; }
    std::optional<int64_t> & get_mutable_estimate() { return estimate; }
    void set_estimate(const std::optional<int64_t> & value) { this->estimate = value; }
};

Each member has a const getter, a mutable getter and a setter. The optional estimate is std::optional<int64_t>, which requires C++17. Arrays become std::vector, and a value that is only null in the sample is kept as nlohmann::json so it can hold anything.

What full mode adds

  • A namespace quicktype with helper functions for reading optional properties safely.
  • adl_serializer specializations in namespace nlohmann for std::optional and std::shared_ptr.
  • from_json and to_json for every class, so j.get<Task>() and nlohmann::json j = task; work.
  • A header comment explaining how to include json.hpp; the file uses #pragma once and #include "json.hpp".
#include "Task.hpp"
auto task = nlohmann::json::parse(text).get<quicktype::Task>();
std::cout << task.get_title() << "\n";

Type mapping

JSONC++
integer / decimalint64_t / double
boolean / stringbool / std::string
arraystd::vector<T>
objecta class
optionalstd::optional<T>
null only / mixednlohmann::json or std::variant

Dates stay std::string; convert them with std::chrono parsing or a date library where needed. Member names use snake_case, the common C++ style, while the conversion functions keep reading the original JSON keys.

Error handling

nlohmann/json reports mismatches with exceptions. Reading a required key that is missing throws nlohmann::json::out_of_range, and a value of the wrong type throws nlohmann::json::type_error; parsing invalid JSON throws parse_error. Wrap the get<Task>() call in a try block and log e.what(), which names the key or position involved. Optional members use std::optional, so an absent value simply leaves them empty instead of throwing.

Getting accurate types

The generator can only see your sample. If a number is always whole in the sample, it becomes int64_t, and a later 2.5 from the server fails to convert. Paste several records, including ones with decimals, nulls and missing keys, so the generated members use double and std::optional where needed.

Tips

  • If you prefer plain aggregates, turn the classes into structs with public members and use nlohmann's NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE macro instead of handwritten functions.
  • Compile with -std=c++17 or newer because of std::optional.
  • Split the output into a header per class if your project prefers that layout.
  • The file is header-only: it starts with #pragma once and defines its helpers inline, so you can save it as Task.hpp and include it from several source files without link errors.

Format the generated code with the C++ Formatter. From XML? Use XML to C++. Check your input with the JSON Validator. Generation happens in your browser.

Frequently asked questions

Which JSON library does the generated C++ code use?
nlohmann/json. Full mode includes from_json and to_json functions and adl_serializer specializations for that library.
Which C++ standard is required?
C++17, because optional values use std::optional.
Why are dates std::string?
C++ has no standard JSON date type, so dates are kept as strings. Parse them with std::chrono or a date library after decoding.
Can I get plain structs without getters and setters?
The generator uses classes with accessors. You can convert them to structs with public members and use nlohmann’s NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE macro.

Last updated September 26, 2026

Related tools

XML to C++ Generate C++ classes with nlohmann::json helpers from a XML sample instantly, with nested types detected automatically. JSON to Crystal Generate Crystal classes with JSON mapping from a JSON sample instantly, with nested types detected automatically. JSON to C# Generate C# classes with JsonProperty attributes from a JSON sample instantly, with nested types detected automatically. JSON to Dart Generate Dart classes with fromJson and toJson from a JSON sample instantly, with nested types detected automatically. JSON to Elm Generate Elm type aliases with decoders from a JSON sample instantly, with nested types detected automatically. JSON to Flow Generate Flow Flow type definitions from a JSON sample instantly, with nested types detected automatically. JSON to Go Generate Go structs with json tags from a JSON sample instantly, with nested types detected automatically. JSON to Haskell Generate Haskell records with Aeson instances from a JSON sample instantly, with nested types detected automatically. JSON to Java Generate Java POJO classes with getters and setters from a JSON sample instantly, with nested types detected automatically. JSON to JavaScript PropTypes Generate JavaScript PropTypes React PropTypes definitions from a JSON sample instantly, with nested types detected automatically. JSON to JSON Schema Generate JSON Schema a JSON Schema document from a JSON sample instantly, with nested types detected automatically. JSON to Kotlin Generate Kotlin data classes from a JSON sample instantly, with nested types detected automatically.