Code Generators
JSON to C++ Converter
Paste a JSON or XML sample — the format is auto-detected — and get typed C++ models with names, nesting and nullability inferred for you.
In short
Convert JSON to C++ online: paste a sample and generate typed C++ models with serialization helpers — powered by quicktype, in your browser.
By NaveenKumar T · Updated
- JSON or XML input, auto-detected
- Nested objects become types
- Nullability inferred
Runs entirely in your browser — nothing you paste or open here is uploaded, logged, or stored.How we handle data →
Example
Example: JSON order payload to C++
A small order document with a nested object, an array of two items, a decimal total and a null field — enough to show how each C++ type, list and nullable is inferred. The same page accepts the XML form of this order too, so the sample below is an example rather than the only accepted format. The right-hand side is the exact output of this tool.
{
"id": "ord_9F2KX",
"status": "processing",
"paid": true,
"customer": { "name": "Ada Lovelace", "email": "ada@example.com", "phone": null },
"items": [
{ "sku": "KB-75", "qty": 1, "price": 149.99 },
{ "sku": "MS-12", "qty": 2, "price": 39.5 }
],
"total": 228.99
}// To parse this JSON data, first install
//
// Boost http://www.boost.org
// json.hpp https://github.com/nlohmann/json
//
// Then include this file, and then do
//
// Root data = nlohmann::json::parse(jsonString);
#pragma once
#include "json.hpp"
#include <boost/optional.hpp>
#include <stdexcept>
#include <regex>
namespace quicktype {
using nlohmann::json;
#ifndef NLOHMANN_UNTYPED_quicktype_HELPER
#define NLOHMANN_UNTYPED_quicktype_HELPER
inline json get_untyped(const json & j, const char * property) {
if (j.find(property) != j.end()) {
return j.at(property).get<json>();
}
return json();
}
inline json get_untyped(const json & j, std::string property) {
return get_untyped(j, property.data());
}
#endif
class Customer {
public:
Customer() = default;
virtual ~Customer() = default;
private:
std::string name;
std::string email;
nlohmann::json phone;
public:
const std::string & get_name() const { return name; }
std::string & get_mutable_name() { return name; }
void set_name(const std::string & value) { this->name = value; }
const std::string & get_email() const { return email; }
std::string & get_mutable_email() { return email; }
void set_email(const std::string & value) { this->email = value; }
const nlohmann::json & get_phone() const { return phone; }
nlohmann::json & get_mutable_phone() { return phone; }
void set_phone(const nlohmann::json & value) { this->phone = value; }
};
class Item {
public:
Item() = default;
virtual ~Item() = default;
private:
std::string sku;
int64_t qty;
double price;
public:
const std::string & get_sku() const { return sku; }
std::string & get_mutable_sku() { return sku; }
void set_sku(const std::string & value) { this->sku = value; }
const int64_t & get_qty() const { return qty; }
int64_t & get_mutable_qty() { return qty; }
void set_qty(const int64_t & value) { this->qty = value; }
const double & get_price() const { return price; }
double & get_mutable_price() { return price; }
void set_price(const double & value) { this->price = value; }
};
class Root {
public:
Root() = default;
virtual ~Root() = default;
private:
std::string id;
std::string status;
bool paid;
Customer customer;
std::vector<Item> items;
double total;
public:
const std::string & get_id() const { return id; }
std::string & get_mutable_id() { return id; }
void set_id(const std::string & value) { this->id = value; }
const std::string & get_status() const { return status; }
std::string & get_mutable_status() { return status; }
void set_status(const std::string & value) { this->status = value; }
const bool & get_paid() const { return paid; }
bool & get_mutable_paid() { return paid; }
void set_paid(const bool & value) { this->paid = value; }
const Customer & get_customer() const { return customer; }
Customer & get_mutable_customer() { return customer; }
void set_customer(const Customer & value) { this->customer = value; }
const std::vector<Item> & get_items() const { return items; }
std::vector<Item> & get_mutable_items() { return items; }
void set_items(const std::vector<Item> & value) { this->items = value; }
const double & get_total() const { return total; }
double & get_mutable_total() { return total; }
void set_total(const double & value) { this->total = value; }
};
}
namespace quicktype {
void from_json(const json & j, Customer & x);
void to_json(json & j, const Customer & x);
void from_json(const json & j, Item & x);
void to_json(json & j, const Item & x);
void from_json(const json & j, Root & x);
void to_json(json & j, const Root & x);
inline void from_json(const json & j, Customer& x) {
x.set_name(j.at("name").get<std::string>());
x.set_email(j.at("email").get<std::string>());
x.set_phone(get_untyped(j, "phone"));
}
inline void to_json(json & j, const Customer & x) {
j = json::object();
j["name"] = x.get_name();
j["email"] = x.get_email();
j["phone"] = x.get_phone();
}
inline void from_json(const json & j, Item& x) {
x.set_sku(j.at("sku").get<std::string>());
x.set_qty(j.at("qty").get<int64_t>());
x.set_price(j.at("price").get<double>());
}
inline void to_json(json & j, const Item & x) {
j = json::object();
j["sku"] = x.get_sku();
j["qty"] = x.get_qty();
j["price"] = x.get_price();
}
inline void from_json(const json & j, Root& x) {
x.set_id(j.at("id").get<std::string>());
x.set_status(j.at("status").get<std::string>());
x.set_paid(j.at("paid").get<bool>());
x.set_customer(j.at("customer").get<Customer>());
x.set_items(j.at("items").get<std::vector<Item>>());
x.set_total(j.at("total").get<double>());
}
inline void to_json(json & j, const Root & x) {
j = json::object();
j["id"] = x.get_id();
j["status"] = x.get_status();
j["paid"] = x.get_paid();
j["customer"] = x.get_customer();
j["items"] = x.get_items();
j["total"] = x.get_total();
}
}
Look for the "phone" field: sampled as null, it is typed as nlohmann::json. "items" became std::vector<Item>, and "total" is double.
Learn more
From JSON sample to C++ types
Hand-writing model classes from a JSON payload is transcription work: read a field, type a property, repeat eighty times, miss one. This page runs quicktype (the same engine behind the best-known generators) against your document, merging array elements to find the common shape and naming nested types from their keys.
The header depends on Boost only for boost::optional. If the project is C++17 without Boost, regenerate with quicktype's no-boost option, which switches the nullable fields to std::optional instead; the nlohmann::json single header stays the only other dependency either way.
How JSON values become C++ types
Inference works from the values in your sample. Strings map to std::string, integers and decimals are told apart (228.99 becomes double, while 1 and 2 stay integral), true/false become the language's boolean, and an object such as "customer" becomes its own named type. The "items" array is typed as std::vector<Item> because every element shares the same keys — quicktype unifies the shapes of all array elements into one Item type.
Nulls are the one place inference needs your help: "phone": null carries no type information, so the field is emitted as nlohmann::json. If the real API sometimes sends a string there, include a sample where the field is populated (paste several documents merged into one array) and the generated type becomes the union of what it saw.
Pasting XML instead: how elements and attributes become C++ types
The XML is parsed into a data model before inference (attributes included, text nodes named "text"), so the root element becomes a nested type: <order> is a field of type Order on the top-level Root rather than the top level itself. Child elements become fields, and repeated elements — the two <item> siblings — are collected into std::vector<Item> under an Items wrapper type that mirrors the <items> container.
Attributes and element text are typed differently, and this matters: element text is type-inferred, so <total>228.99</total> becomes double, while attribute values are kept as they were written — qty="1" and price="149.99" stay std::string in C++. Prefer element text for numeric data you want typed, or cast attribute values after decoding. One more trap: a single <item> would not be recognized as a list, so make sure repeated elements appear at least twice in the sample.
Annotations, nulls and nested arrays in the generated C++
C++ has no annotations, so quicktype writes free from_json and to_json overloads inside namespace quicktype, which nlohmann::json finds by argument-dependent lookup — that is why parsing straight into Root works with no registration step. The classes keep private fields behind get_x, get_mutable_x and set_x accessors, and the header opens with #pragma once and an include of json.hpp. Helper templates travel with it: get_untyped for fields with no inferred type, get_stack_optional and get_heap_optional, and an adl_serializer specialisation so boost::optional serializes correctly.
A field sampled only as null is typed nlohmann::json and read through get_untyped, which returns an empty json when the key is missing rather than throwing. Add a document where the field carries a value and it becomes boost::optional<std::string>, read with get_stack_optional — the reason the header includes boost/optional.hpp even when your sample has no optionals. Keys absent from some merged samples get the same boost::optional treatment, and get_heap_optional is generated alongside for the cases quicktype needs a heap-allocated optional instead.
Each nested object becomes its own class in namespace quicktype — Customer, Item — with matching from_json and to_json overloads whose prototypes are declared ahead of the inline definitions, and the classes themselves are emitted dependency-first so Root can hold them by value. Arrays of objects are std::vector<Item>, read in one call that recurses into the element overload. As everywhere, a one-element array names the class without proving anything, and an empty array leaves you with a vector of untyped nlohmann::json values.
Help
Frequently asked questions
Everything you need to know about the JSON to C++.
01How do I generate C++ classes from JSON?
Paste a representative document — JSON or XML, the format is detected automatically — and press Convert. A real API response works best. Nested objects become their own C++ types, arrays become std::vector<Item>, and the result downloads as Root.hpp.
02What does the generated C++ code include?
C++ generation targets nlohmann::json — structs with to_json/from_json functions, std::optional for nullables, and helpers handling nested types — modern C++17 models from one paste.
03How do I use the generated C++ code in my project?
Include nlohmann/json (json.hpp) and Boost's optional header, then parse with Root data = nlohmann::json::parse(jsonString); — the generated from_json and to_json overloads let nlohmann's ADL serialization convert in both directions. The structs are plain C++17 with getters and setters.
04Which JSON library does the C++ output depend on?
nlohmann/json (the 'JSON for Modern C++' single header) — the community default. The generated to_json/from_json overloads integrate with its ADL serialization, so usage is just j.get<Root>().
Keep working