Skip to main content

JSON to C++ Struct

Paste JSON and get C++ structs with nlohmann::json bindings and std::optional for nullable fields. Private, in your browser.

Paste JSON on the left. Add a second sample to find optional fields.

JSON input

Stays in your browser

C++ code

The generated code appears here.

Shortcuts: Ctrl+Enter run · Ctrl+Shift+C copy result · / search (use ⌘ on Mac)

Next steps

Free Online JSON to C++ Struct Generator

Generate C++ structs that convert to and from JSON with the nlohmann::json library. It gives developers a quick browser-based way to solve a focused json to c++ struct task before moving the result into a project.

How to generate C++ code from JSON

Paste a JSON object or an array of objects into the input box and press Generate. Keys that are valid C++ names use the NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE macro. Keys that are not get hand-written to_json and from_json functions. Nullable fields use std::optional.

One input, every language

Your JSON stays in the box when you switch the language tabs, and the code is generated again at once. Each language also has its own page, so you can bookmark the one you use.

How types are worked out

  • Whole numbers and numbers with decimals are told apart.
  • Add more samples with Add sample: a key missing from some samples becomes optional, and null makes a type nullable.
  • Arrays of objects are merged into one type, and nested objects become their own types named after the key.
  • Keys that are not valid names in the language (such as first-name or 2fa) are renamed and mapped back, never rejected.

Worked example

The sample JSON in the box above, generated with the default options:

{
  "id": 1,
  "name": "Ada",
  "email": null,
  "tags": ["x"],
  "address": { "city": "Pune", "pin": "411001" },
  "createdAt": "2026-01-15T10:30:00Z"
}
#pragma once
#include <nlohmann/json.hpp>
#include <cstdint>
#include <string>
#include <vector>
#include <optional>

struct Address {
    std::string city;
    std::string pin;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Address, city, pin)

struct Root {
    int64_t id;
    std::string name;
    std::optional<std::string> email;
    std::vector<std::string> tags;
    Address address;
    std::string createdAt;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Root, id, name, email, tags, address, createdAt)

Common mistakes

  • A single sample cannot show which fields are optional. Paste two or three different responses for a realistic result.
  • A value that is always null gives no type information, so it is treated as a nullable string. Change it by hand if it is something else.
  • Numbers are typed from the samples: 1 and 1.0 look the same in JSON, so use a sample with decimals where you need a float.

Frequently asked questions

Which library does the output need?
nlohmann/json, a single header library. The output includes the include line.
Can nlohmann handle std::optional?
Recent versions (3.11 and newer) handle std::optional members. For older versions turn the option off to use plain types.
Why are nested structs listed first?
C++ needs a type to be defined before it is used, so the nested structs come before the struct that holds them.
Is my JSON uploaded?
No. The types are worked out in your browser. Nothing you paste is sent to a server.
How do I use this json to c++ struct tool?
Use the interactive controls above; processing stays in your browser and the result is intended as a practical starting point for development work.

More Formatters & Parsers

View all Formatters & Parsers tools →