DT Dev Tools

JSON to C++ Struct Generator

Convert JSON to C++ structs or classes online with nlohmann/json from_json and to_json, std::optional fields and namespaces. Free, instant and private in your browser.

🔒 Runs entirely in your browser — nothing is uploaded

Advertisement

Generate C++ types from JSON

C++ has no built-in reflection, so mapping JSON onto your own types normally means writing a struct and then a pair of serialization functions for every object in the payload. This JSON to C++ converter generates both from a sample. Each JSON object becomes a struct (or a class with getters and setters), keys become snake_case members, nested objects become their own types, arrays become std::vector<T>, integers become int64_t, fractional numbers become double, and fields that are missing or null in part of the sample become std::optional<T>. Everything is wrapped in the namespace you choose and emitted as a single header guarded by #pragma once.

Type inference is done by quicktype, which merges all elements of each array before choosing a type. A field that appears in only some items is still captured as optional, so parsing the full response does not throw on a missing key.

nlohmann/json integration

The generated header is designed for nlohmann/json, the most popular JSON library for modern C++. For every type it defines from_json and to_json overloads that map members back to the original JSON keys, plus small helpers for optional values. Once json.hpp is available, reading a document is a single call to nlohmann::json::parse(text).get<Root>(), and writing it back is nlohmann::json j = value;. Tick Types only if you only need the declarations — for example to use with a different serializer — and Use Boost if your toolchain cannot use C++17.

Tips for production code

Paste a complete sample with several array items and optional fields filled in so the types are accurate. Empty arrays and nulls fall back to nlohmann::json, which you will usually want to replace. Rename the root type to match your domain, set a namespace that fits your project, and run clang-format on the result. Review integer widths, consider enum class for strings with a fixed set of values, and note that date-time strings stay std::string so you can parse them with the library of your choice. All processing happens locally, so internal payloads stay on your machine.

How to use

  1. Paste JSONPaste a JSON document or click Load sample.
  2. ConfigureSet the root type name, namespace, struct or class style and optional support.
  3. Add the headerCopy the code or download a .hpp file and include it next to json.hpp.

Frequently asked questions

Which JSON library does the generated code use?
It targets nlohmann/json. The header includes from_json and to_json overloads, so nlohmann::json::parse(text).get<Root>() just works.
Which C++ standard is required?
C++17 by default, because optional fields use std::optional. Tick Use Boost to use boost::optional instead if you are stuck on an older standard.
Can I get classes with getters and setters?
Yes. Switch Types to classes with getters/setters for encapsulated members, or keep plain structs for simple data holders.
Is my JSON uploaded?
No. The header is generated entirely in your browser; nothing is sent to a server.
Advertisement