Skip to main content
Convert2JSON Tools

JSON to C++ Converter

Generate modern C++ structs from a JSON sample, using the standard library types you would write by hand.

Your data stays on your device

Input — JSON

characters
0
lines
0
size
0 B

Output — C++

characters
0
lines
0
size
0 B

Options

About this tool

Modelling JSON in C++ means choosing types: std::string for text, std::vector for arrays, and std::optional for values that may be absent. Getting the declaration order right also matters, since a struct must be defined before it is used as a member.

The generator emits nested structs first, in dependency order, so the file compiles as written. Optionally it adds nlohmann/json serialisation macros, which give you conversion in both directions with a single line per struct.

How to use this tool

  1. Paste your JSON sample.
  2. Set the root struct name.
  3. Choose whether to emit nlohmann/json macros and whether to use std::optional for nullable fields.
  4. Press Generate and copy the structs into a header file.

Key features

  • std::string, std::vector and std::optional used idiomatically
  • Nested structs emitted in dependency order so the file compiles top to bottom
  • Optional NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE macros for serialisation
  • int64_t and double distinguished from the sample
  • C++ keywords and invalid identifiers renamed safely
  • Optional include block for the standard headers used

Example

Struct with optional field

InputJSON

{"count":12,"label":null}

OutputC++

struct Root {
    int64_t count;
    std::optional<std::string> label;
};

Good to know

  • The standard library has no JSON support, so serialisation requires a third-party library such as nlohmann/json.
  • Heterogeneous arrays cannot be typed and are reported rather than silently converted.
  • std::optional requires C++17 or later.

Frequently asked questions

Which JSON library should I use?

nlohmann/json is the most common choice for its ergonomics. RapidJSON is significantly faster for large payloads but far more verbose to use.

Why is declaration order important?

C++ requires a complete type before it can be used as a member by value. The generator sorts structs so every dependency appears first.

std::optional or a default value?

std::optional when absence is meaningful and you want the compiler to force you to handle it. A default value is simpler but hides the distinction.

Is a header guard included?

A #pragma once is emitted when you enable the include block. Add a traditional guard if your project prefers one.

Your data stays on your device

Your data is processed locally in your browser and is not uploaded to our server. This page keeps working after you go offline, because there is nothing to send.

Share this toolXLinkedInHacker News