JSON to C++ Converter

JSON Input

C++ Output

Paste your JSON on the left and the C++ code shows up here.

Or drop a file on this box, , or .

What kind of C++ header do you get?

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.

Integer widths follow the values: int until a number passes 32 bits, then std::int64_t or std::uint64_t, so a PLC reporting "bootCount": 5000000000 gets a 64-bit member. Decimals are double, text is std::string, true/false is bool. Each array turns into a std::vector<T>, an array of arrays nests the vectors ([[1,2],[3,4]] is std::vector<std::vector<int>>), and a nested object lives inside its parent by value, declared ahead of it, so the page Sample compiles with g++ -std=c++17 as generated. A key holding null, or carried by only some array elements, is wrapped in std::optional, and that wrapper is why C++17 is the floor.

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.

1

Paste a JSON sample

JSON Input takes a paste, Upload takes a .json or .txt file, and Sample fills in a subscriber with an account block and a device list. An order with an unused coupon shows most of the mapping in one line:

{"orderId":"ORD-1001","total":59.9,"quantity":2,"paid":true,"coupon":null,"customer":{"name":"Ava Chen"},"items":[{"sku":"SKU-101","price":19.95}]}

All array elements are inspected, not only the first, so a label that only the second sensor reports becomes std::optional<std::string> label;. A root that is a bare number or a list of strings gets a "Nothing to generate" message, and a root array of objects earns an extra using RootObjectList = std::vector<RootObject>; line at the bottom.

2

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.

3

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?

With only null to go on, as in "lastFault": null, the member is written as std::optional<std::string> last_fault; and its comment ends "only null in the sample: pick the real type". An array like [1, "x", {"a": 1}] is typed std::vector<std::string> with the comment "mixed types in the sample: keep the raw JSON text", because nothing in the standard library can hold an arbitrary JSON value.

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

Writing the same payload for a C module, a Go service or a TypeScript front end? Each of these reads the same JSON and speaks that language's types: