C++ to JSON Converter
Paste C++ structs or objects. Get clean JSON back.
What this tool does
Writing a JSON body that matches a C++ aggregate usually means adding nlohmann::json, declaring NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE or a hand-written to_json, and sitting through a rebuild just to see one example. Skip that loop: paste the structs and an instance, and the backend replies with JSON for that instance, nested structs, std::vector members and an empty std::optional included.
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.
C++20 designated initializers (.orderId = "ORD-4821") work, and so do plain brace lists like { "SKU-101", 2, 49.99 }, which are lined up with the members in declaration order. Paste several aggregates at once and each lands under a key of its own. If you know Boost.JSON, think of the result as roughly what boost::json::value_from would print, minus the tag_invoke overloads.
How to use it
Getting from a header to a JSON body takes three steps, whether the paste is one aggregate or a dozen interlinked types.
Paste your C++ (or load the sample)
Paste C++ into the left editor without trimming it: #include lines, struct or class definitions, an aggregate initializer, nested types. Load Sample shows an Order with a std::vector<OrderItem>, an Address built from a positional brace list, and notes set to std::nullopt.
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
Hit the green Convert button. Your C++ travels to the backend, which walks the instance member by member and answers with JSON; a "Converting..." overlay covers the panels until the reply arrives.
Copy the JSON
The result appears, indented, in the read-only right panel. Select it and paste it into a curl command, a GoogleTest fixture file or the README next to your CMakeLists.txt. Edit the left side and convert again as often as you like; the previous output is cleared first.
When this actually comes in handy
API request payloads
Your C++ client fills a request struct before handing it to libcurl or cpr. Convert an instance of it and you have a body to replay in Postman while you debug the server.
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
Keep the sample payloads in your API reference honest by generating them from the structs the service actually compiles, instead of retyping them every release.
Test fixtures and seeds
Aggregate initializers in your Catch2 or GoogleTest cases already describe valid objects. Turn them into JSON seed data for an integration environment, a mock server or a database loader.
Common questions
Do I need nlohmann::json or Boost installed?
No. The source is never compiled or run; the backend reads the text and writes JSON. For runtime serialization in your own build, nlohmann/json is the header-only favourite, and Boost.JSON is worth a look if Boost is already in your dependency tree.
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, a whole header or .cpp file is fine. Every top-level aggregate gets a separate key, nested types are written out inside it, and members a struct gets from its base class sit next to its own.
Is my code stored?
The source travels to the backend, gets converted, and is not stored afterwards. Proprietary headers can carry license keys or internal hostnames in constants, so check for those before pasting.
What if the code has templates, macros, or lambdas?
Templates instantiated with concrete types are handled. Code with no data shape, like an uninstantiated template, a macro body or a lambda, is written as null so the rest of the object still converts. A missing semicolon or an unbalanced brace can throw the read off, so clear compiler errors first: this is not a full C++ front end.
Other tools you may need
Projects that mix C++ with C or C#, plus the checks worth running on the output, are covered here: