JSON to C++ Converter
Turn a JSON sample into C++17 structs with std::string, std::vector and std::optional members
JSON Input
C++ Output
What does the JSON to C++ converter generate?
When you map a JSON payload onto C++ types, the boring part is writing the structs: one per object, the right container for every array, and the right integer width for every number. This page does that part. Paste a sample and you get a header that starts with #pragma once, includes only what it uses (<string>, <vector>, <optional>, <cstdint>), and declares plain aggregate structs inside a namespace.
Strings become std::string, decimals double, booleans bool, and whole numbers int, or std::int64_t / std::uint64_t once a value passes 32 bits. Arrays become std::vector<T>, and nested arrays nest the vectors ([[1,2],[3,4]] is std::vector<std::vector<int>>). Nested objects are held by value as their own structs, which are written before the struct that uses them, so the page Sample compiles with g++ -std=c++17 straight away. Fields that were null, or missing from some array elements, are wrapped in std::optional, which is why C++17 is the minimum.
Member names are snake_case, and C++ keywords get a trailing underscore (class becomes class_). Each member whose name differs from its JSON key carries a // JSON key "billingCycle" comment, which comes in handy when you write the mapping code. That mapping is the part this page doesn't do: there are no to_json/from_json functions and no serialization macros, so you hook the structs up to nlohmann/json, RapidJSON or whatever parser your project already uses. Everything runs in your browser.
How to use it
Paste, check, copy. The output refreshes a moment after you stop typing.
Paste a JSON sample
Paste JSON into the JSON Input panel, Upload a .json or .txt file, or click Sample. For example:
{"orderId":"ORD-1001","total":59.9,"quantity":2,"paid":true,"coupon":null,"customer":{"name":"Ava Chen"},"items":[{"sku":"SKU-101","price":19.95}]}Every element of an array is looked at, not just the first, so keys that appear in only some objects end up as std::optional members. The root must be an object or an array of objects; for a root array you also get a using RootObjectList = std::vector<RootObject>; alias.
Pick the names and read the output
Struct Name renames the root struct. Use Namespace wraps everything in a namespace (default models; acme::models works too). For the example above the root struct gets std::string order_id, double total = 0.0, int quantity = 0, bool paid = false, std::optional<std::string> coupon, Customer customer and std::vector<Item> items.
Copy or download
Copy puts the header on your clipboard; Download saves it as RootObject.hpp. Clear resets the input.
Where this saves time
Typing a REST response before you parse it
You call an orders endpoint and want typed access instead of chains of j["customer"]["name"]. Generate the structs, then write the small from_json overloads your library expects.
Game and app config files
Level data or app settings stored as JSON map nicely onto value-type structs. Numeric and bool members get default initializers, so a freshly constructed config is never full of garbage.
Catching 64-bit IDs
Snowflake IDs and millisecond timestamps don't fit in an int. The converter checks every value and picks std::int64_t or std::uint64_t where needed, and keeps integers too big for 64 bits as std::string.
Reviewing a message format
Paste a sample message from another team and you get a quick, compilable view of its shape, handy in a pull request or design doc.
Common questions
Does it generate nlohmann/json or RapidJSON code?
No. The output is struct declarations only: no to_json/from_json, no NLOHMANN_DEFINE_TYPE_* macros and no RapidJSON handlers. Add the binding code for the library you use; the // JSON key comments tell you which key goes where.
Which C++ standard do I need?
C++17, because of std::optional and nested namespace names like acme::models. The generated headers are checked with g++ -std=c++17 -Wall -Wextra -pedantic.
Why are nested objects held by value?
A by-value member keeps the struct a simple aggregate that you can copy and compare without managing memory. If an object can be null or missing, it becomes std::optional<Customer> instead. Switch to std::unique_ptr by hand if you need recursive types.
What happens to null and mixed values?
A key that was only ever null becomes std::optional<std::string> with a comment asking you to pick the real type. An array mixing numbers, strings and objects becomes std::vector<std::string> with a note to keep the raw JSON text, since the standard library has no JSON value type.
Can I use it for C instead?
Use the JSON to C page. It writes typedef struct definitions with pointers and size_t counts instead of std::string and std::vector.
Is my JSON sent to a server?
No. The structs are generated by JavaScript running in this page, so the input never leaves your browser. Inputs over 256 KB are refused; trim long arrays to a few items first.
Other Code Generation Tools
Need the same payload in another language, or want to clean it up first?