JSON to C++ Converter Online

Generate C++17 structs from any JSON payload — std::string, std::vector, primitives, and nested types. Compatible with nlohmann/json. 100% in your browser.

What is a JSON to C++ converter?

A JSON to C++ converter reads a JSON sample and emits typed struct declarations using std::string, std::vector, primitives, and nested types — exactly what nlohmann/json, rapidjson, or simdjson can deserialize into. The output drops straight into a C++17 project and works out of the box once you add the macro that wires up to_json and from_json.

Hand-rolling C++ DTOs from a 200-field API payload is slow and error-prone — typos in field names break compilation only at the call site, and missing fields fall back silently with default values. OpenFormatter walks the JSON tree once, generates idiomatic C++17, and runs entirely in your browser so internal API responses never leave your machine.

How to generate C++ structs from JSON — 4 steps

  1. Paste a representative JSON response. Type inference reads the first observed value of each field — booleans must be true/false, numbers must be unquoted.
  2. Click Convert. The tool emits a Root struct plus one separate struct per nested object, with std::string and std::vector typing.
  3. Copy and paste into your project. Each struct is independent; you can split them across header files in the same namespace.
  4. Wire up nlohmann/json. Add NLOHMANN_DEFINE_TYPE_INTRUSIVE(Root, ...fields) inside each struct, then call nlohmann::json::parse(s).get<Root>().

JSON to C++ type mapping

Sample JSON

{
  "id": 42,
  "name": "Ada Lovelace",
  "active": true,
  "score": 98.5,
  "address": { "city": "London" },
  "tags": ["admin", "engineer"]
}

Generated C++

#include <string>
#include <vector>
#include <any>

struct Address {
    std::string city;
};

struct Root {
    int id;
    std::string name;
    bool active;
    double score;
    Address address;
    std::vector<std::string> tags;
};

C++17 structs

Aggregate-initializable POD-compatible structs using std::string and std::vector — exactly what nlohmann/json and rapidjson expect.

Nested structs

Every nested JSON object becomes its own struct held by value — no pointers, no manual memory management.

Client-Side Only

JSON is parsed in JavaScript inside the browser. Internal API responses, tokens, or PII never reach a server.

Common use cases

  • check_circleGenerating DTOs for a Drogon, Crow, or Pistache HTTP API written in C++
  • check_circleModelling JSON config files (settings.json) loaded at startup with nlohmann/json
  • check_circleProducing message types for a ZeroMQ or NATS consumer that exchanges JSON payloads
  • check_circleGenerating game engine data classes (Unreal / Unity native plugin) from server JSON
  • check_circleModelling REST API responses for a Qt desktop application using nlohmann/json
  • check_circleBuilding protocol buffers replacement structs for embedded systems with simdjson
  • check_circleBridging a Python or Node service to a C++ analytics core via JSON-on-the-wire
  • check_circleGenerating gRPC or Cap’n Proto fallback types when JSON is the lowest common denominator

Why client-side generation matters

The JSON you paste often contains real customer data, OAuth tokens, signed URLs, or internal field names you do not want indexed by a third-party converter. OpenFormatter generates the C++ entirely in JavaScript on your device — open the Network tab in DevTools and you will see zero requests when you click Convert. Safe for enterprise use, safe behind a corporate proxy, safe when the API contract is itself confidential.

Need other native tooling?

Generate Rust structs, Go types, or browse other JSON converters — all browser-side.

Frequently Asked Questions

What C++ standard is targeted?

C++17 — the generated code uses std::string, std::vector, and std::any, all of which are part of the C++17 standard library. std::any is used for nullable JSON values; if you target C++14 replace it with void* or boost::any. The structs themselves are POD-compatible (Plain Old Data) so they can be aggregate-initialized.

Should I use std::variant for mixed-type fields?

Yes when a JSON field can legitimately hold multiple types — std::variant<std::string, int, std::nullptr_t> models a "string or integer or null" field exactly. The generator does not infer variants from a single sample (it picks the type of the first observed value), so add std::variant manually after generation when you know a field is polymorphic.

What about nlohmann/json for parsing?

nlohmann/json (single-header, MIT-licensed) is the most popular C++ JSON library and supports automatic struct serialization with NLOHMANN_DEFINE_TYPE_INTRUSIVE(StructName, field1, field2). Add that macro inside each generated struct and then nlohmann::json::parse(text).get<Root>() works out of the box. Other options: rapidjson (faster, lower-level) and simdjson (fastest parsing, read-only).

How are nested JSON objects handled?

Each nested JSON object becomes its own top-level C++ struct. The parent struct holds it by value (not pointer) since C++ aggregates compose naturally — no manual memory management. Forward declarations are not needed because nested structs are emitted before their referencing parent.

How are JSON arrays mapped?

A JSON array becomes std::vector<T> where T is inferred from the first element. Arrays of objects become std::vector<ChildStruct>; arrays of strings become std::vector<std::string>. For arrays of mixed types use std::vector<std::any> after generation.

Why structs and not classes?

In C++, struct and class are nearly identical — the only difference is default member visibility (public for struct, private for class). DTOs typically expose all fields publicly, which makes struct the idiomatic choice and removes the need to write public: explicitly. Convert to class with private + public methods if you need encapsulation.

Is the JSON uploaded to your servers?

No. Conversion runs entirely in your browser via JavaScript. Open DevTools → Network and click Convert — no requests are made. Pasting JSON containing tokens or proprietary data never leaves your machine.

How do I serialize back to JSON?

With nlohmann/json: add NLOHMANN_DEFINE_TYPE_INTRUSIVE(Root, id, name, active) inside each struct, then nlohmann::json j = root; produces the JSON. The library uses argument-dependent lookup to find the macro-generated to_json/from_json functions automatically.

JSON to C++ Converter Online — Generate Structs