YAML to C++
Paste YAML and instantly generate C++ struct definitions with inferred types for every field.
Example
Input YAML:
name: Ada
age: 36
skills:
- C++
- math
address:
city: London
zip: 11223
Generated C++:
#include <string>
#include <vector>
struct Address {
std::string city;
long long zip;
};
struct Root {
std::string name;
long long age;
std::vector<std::string> skills;
Address address;
};How it works
The tool parses your YAML with js-yaml, walks the resulting value to infer a type model, and emits C++ structs (using std::string, std::vector, and nested structs). The root type is named Root.
Good to know
YAML to C++ turns a YAML document into ready-to-paste C++ struct definitions, guessing a concrete type for every field as it reads your data. It's aimed at C++ developers who already have config files, API samples, or fixture data in YAML and want a typed model to deserialize into, rather than hand-writing each struct and chasing down the right types by trial and error.
Reach for it when you're wiring up a YAML config loader (with a library such as yaml-cpp), prototyping a data model from a sample payload, or just sanity-checking how a nested document maps onto structs. The generated code always starts from a struct named Root and pulls in <string> and <vector>, so it compiles on its own without extra headers.
To read the output, remember that types are inferred from values, not from any schema, so accuracy depends entirely on your sample. A few things worth knowing as you scan the result:
- Whole numbers become
long long and decimals become double, so an integer-looking sample (for example 1843) will never be typed as a float even if real data sometimes is. - Empty arrays and
null values fall back to std::string because there's nothing to infer from; supply a non-empty, representative example to get the type you actually want. - When a list mixes numeric and non-numeric values, or merged object keys disagree, the tool widens the type to
std::string rather than guessing.
A practical tip: feed it the richest example you have, with every optional field populated and arrays containing at least one element, then treat the structs as a fast first draft. You'll typically still want to swap long long for a tighter integer type, decide where std::optional belongs, and rename the auto-generated Root and nested struct names to match your domain.
Frequently asked questions
What C++ types are generated?
Strings become std::string, whole numbers become long long, decimals become double, booleans become bool, arrays become std::vector, and nested mappings become their own struct. Null or unknown values default to std::string.
How are arrays of objects handled?
When every item in a YAML sequence is a mapping, the tool merges their keys into a single struct and emits std::vector, so heterogeneous records still produce one clean element type.
Is my data uploaded anywhere?
No — this tool 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 YAML into these C++ structs at runtime?
This tool only generates the struct definitions, not the parsing code. To load actual data you'd pair the structs with a runtime YAML library such as yaml-cpp, reading each node and assigning it to the matching field.
What C++ standard do the generated structs require?
The output uses std::string and std::vector, which have been available since C++98/C++11, so it compiles under any modern standard. If you later switch fields to std::optional you would need C++17 or newer.
Why is my numeric field typed as long long instead of int?
The tool maps every whole number to long long to avoid overflow on large values, since it cannot know the intended range from a single sample. You can safely narrow it to int, int32_t, or another integer type if your data fits.
Does the tool generate JSON or YAML serialization code for the structs?
No. It emits plain struct definitions only, with no constructors, serialization, or reflection helpers. You add any (de)serialization logic yourself or via a separate library.
Can it handle a YAML file whose top level is a list instead of a map?
Yes. If the root is a sequence, it wraps the result in a Root struct containing a single field named items typed as a std::vector of the inferred element type.
How are duplicate or differing keys across array objects resolved?
For a sequence where every item is a mapping, the tool merges all keys into one struct. If the same key shows up with different value types, it widens to a compatible type, falling back to std::string when they don't reconcile.
Is YAML to C++ safe to use with private or proprietary config files?
Yes. The conversion runs entirely in your browser using the js-yaml library, and nothing is uploaded to a server. It also works offline once the page has loaded.
Why did fields with special characters get renamed in the output?
C++ identifiers can only contain letters, digits, and underscores and can't start with a digit. The tool sanitizes keys by replacing invalid characters with underscores and prefixing a leading digit with an underscore.
Related tools