C++ to JSON Converter
Paste C++ structs or objects. Get clean JSON back.
What this tool does
If you have ever needed a JSON payload that mirrors a C++ struct — to feed an API, a fixture, or a config file — you know the drill: pull in nlohmann::json, write the to_json glue, and rebuild. Paste the C++ here and get valid JSON back without any of that. It handles a single initializer, a full header file with several structs, or something with nested templates — same result: a clean JSON document with every field preserved.
The converter understands how C++ values actually serialize. std::string becomes a JSON string, numeric literals like 49.99f or 42u become JSON numbers without the suffix, bool becomes true/false, and std::vector<T> / std::array<T,N> become JSON arrays. std::optional<T> with std::nullopt drops to null; with a value it serializes the inner type. std::map and std::unordered_map with string keys become JSON objects, per cppreference conventions.
Designated initializers from C++20 (.field = value) are handled, as are aggregate initializer lists. Paste several structs at once and each one comes out as a top-level entry in the JSON, with nested types expanded. If you are coming from the Boost.JSON world, the output matches what you would expect from boost::json::value_from — just without the template acrobatics.
How to use it
Three steps. Works whether you paste a single initializer list or a whole header with a dozen structs.
Paste your C++ (or load the sample)
Drop your C++ into the left editor as-is. A struct definition, a class, an initializer list, multiple types, or nested structures — all fine. Click Load Sample to see a realistic example first.
You do not need to strip #include directives, namespaces, or templates. Leave the C++ syntax intact — the parser handles it. For reference on the standard, ISO C++ keeps the official materials.
Hit Convert
Click the green Convert button. The tool reads the C++, preserves every struct and field, and emits the JSON in one pass. A short loading indicator shows while it runs.
Copy the JSON
The right panel fills with indented JSON. Copy it into your REST call, test fixture, CMakeLists-adjacent config, or documentation. That is the whole loop.
When this actually comes in handy
API request payloads
You have a C++ request struct and need JSON to drop into curl or Postman. Paste the struct, grab the JSON, move on.
Config file templates
A Settings struct with 40 members turns into a ready-to-edit JSON template — no handwritten scaffolding, no forgotten keys.
Docs that match the code
Generate JSON examples for a README or API reference straight from your real structs, so the docs stop drifting from reality.
Test fixtures and seeds
Turn the initializer lists from your unit tests into JSON seed files for integration tests, mock servers, or database loaders.
Common questions
Do I need nlohmann::json or Boost installed?
No. This tool runs server-side and does not execute your code — you get JSON without pulling in a serialization library. If you do want a runtime library, nlohmann/json and Boost.JSON are both solid choices.
Does it handle std::optional, std::variant, and smart pointers?
std::optional<T> with std::nullopt becomes null; with a value, the inner type is serialized. std::unique_ptr and std::shared_ptr are treated the same way — null if empty, the pointed-to value otherwise. std::variant uses the currently active alternative.
What about containers like std::vector and std::map?
std::vector, std::array, std::list, std::set, and std::deque all become JSON arrays. std::map / std::unordered_map with string keys become JSON objects; with non-string keys the keys are stringified per cppreference container semantics.
Can I paste multiple structs at once?
Yes — paste a whole header or a .cpp file. Each top-level aggregate comes out as its own key in the JSON, with nested types expanded. Inherited members from base classes are included too.
Is my code stored?
Your code is sent to the backend for conversion and is not persisted — we do not log the payload. As always, if the code is genuinely sensitive, give it a quick review before pasting.
What if the code has templates, macros, or lambdas?
Templates with concrete types work fine. Uninstantiated templates, macros, and lambdas get emitted as null rather than failing the whole run, so the rest of your data still comes through. Fix obvious syntax errors first — the parser is forgiving but not a full compiler.
Other tools you may need
C++ to JSON is one piece of the puzzle. These pair well with it: