JSON to C++
Paste JSON and instantly get C++ struct definitions with inferred types, ready to drop into your project.
Example
Input JSON:
{ "id": 1, "name": "Ada", "address": { "city": "London" }, "tags": ["a", "b"] }Generated C++:
struct Address {
std::string city;
};
struct Root {
int id;
std::string name;
Address address;
std::vector<std::string> tags;
};How it works
It parses your JSON, infers a C++ type for each value (int, double, bool, std::string, std::vector, nested struct), and emits struct definitions in dependency order with the entry point named Root.
Good to know
JSON to C++ turns a sample JSON document into ready-to-compile C++ struct definitions. It walks every key, infers a C++ type for each value, and writes out one struct per object plus a top-level Root entry point, prefixed with the includes you'll need (<string>, <vector>, <cstdint>, and <optional>). It's aimed at C++ developers who receive data as JSON from an API, config file, or another service and want a typed model to deserialize into without hand-writing the boilerplate.
Reach for it when you're scaffolding the data layer for a new endpoint, porting a payload into a strongly typed codebase, or just want a quick first draft of the structs before you wire up a parser like nlohmann/json or RapidJSON. Paste a representative JSON sample, click Convert, and copy the result; everything runs locally in your browser, so it's safe to use with internal or sensitive payloads.
To read the output, start at the bottom: Root is your entry point, and any nested object becomes its own struct defined above the struct that uses it, so the file compiles top-to-bottom with no forward declarations. Type inference follows a few simple rules worth knowing:
- Whole numbers become
int, or int64_t if they exceed the 32-bit signed range; numbers with a decimal point become double. - Arrays become
std::vector<T> where T comes from the first element, and a null value is emitted as std::optional<std::string> since its real type can't be known.
The big caveat is that inference only sees the sample you paste, so feed it a complete, representative record. An empty array defaults to std::vector<std::string>, a null guesses at optional<std::string>, and only the first array element is inspected, so mixed-type or sparse arrays may need a manual fix. Treat the generated structs as a strong starting point and adjust the few fields where your data is richer than the sample.
Frequently asked questions
How are numbers mapped to C++ types?
Integer values become int (or int64_t when they exceed the 32-bit signed range), and any value with a decimal point becomes double.
In what order are the structs emitted?
Nested object types are defined before the structs that use them, so the output compiles top-to-bottom without forward declarations, with the entry point always named Root.
Is my data uploaded anywhere?
No — it runs entirely in your browser. Your input never leaves your device and it works offline once loaded.
Is it free?
Yes, completely free with no sign-up and no limits.
People also ask
How do I parse JSON into these structs in C++?
The tool only generates the struct definitions, not the parsing code. You'd typically pair them with a library such as nlohmann/json or RapidJSON and write or generate the deserialization that maps JSON fields onto each struct member.
What C++ type does a null value become?
A null is emitted as std::optional<std::string> because its actual type cannot be inferred from a null alone. If you know the field's real type, change the std::string inside the optional accordingly.
How are arrays of objects handled?
An array of objects becomes std::vector<T>, where T is a struct generated from the first element. The element struct is named by singularizing the field name (for example, a "projects" array yields a Project struct).
Does it reuse a struct when two objects have the same shape?
Yes. Objects with identical field names and inferred types are deduplicated into a single struct, so the same shape appearing in multiple places does not produce duplicate definitions.
What happens if my top-level JSON is an array instead of an object?
When the root is an array, the tool emits a type alias like using Root = std::vector<...>; instead of a Root struct, with any element structs defined above it.
How are invalid C++ field names from JSON keys handled?
Characters that aren't letters, digits, or underscores are replaced with underscores, and a leading digit gets an underscore prefix, so keys like "first-name" or "2nd" become valid identifiers such as first_name and _2nd.
Will it produce int or int64_t for large numbers?
Whole numbers within the 32-bit signed range map to int, and values whose absolute value exceeds 2147483647 map to int64_t. Any number containing a decimal point maps to double.
Is the generated C++ ready to compile as-is?
It includes the needed headers and orders structs so there are no forward-declaration issues, so it generally compiles. You may still want to review fields inferred from null, empty arrays, or non-representative samples.
Related tools