Skip to content
XUtilHub

JSON to C++ Struct Generator (nlohmann/json)

Convert JSON to C++17 structs with from_json and to_json functions for nlohmann/json. std::optional for nullable fields, std::vector for arrays.

  • Free, no sign-up
  • Runs in your browser, nothing uploaded
Input
Code

About the JSON to C++

Generate a C++17 header with structs and from_json/to_json functions for the popular nlohmann/json library. Nullable and optional fields use std::optional and arrays use std::vector.

Key features

  • C++17 header with structs for each JSON object
  • from_json / to_json functions for nlohmann/json
  • std::optional for nullable and optional fields
  • std::vector for arrays, int64_t and double for numbers
  • Value-initialized members ({}), so no garbage values
  • Optional namespace; C++ keywords avoided

C++ structs for nlohmann/json

C++ has no built-in reflection, so mapping JSON to structs means writing serialization code by hand. Paste a sample and get a complete header: structs plus from_json/to_json functions for nlohmann/json.

Example

[
  { "user_id": 7, "name": "Ada", "nickname": "ada", "score": 4.5 },
  { "user_id": 8, "name": "Alan", "nickname": null, "score": 3 }
]
struct User {
    int64_t user_id{};
    std::string name{};
    std::optional<std::string> nickname{};
    double score{};
};

inline void from_json(const nlohmann::json& j, User& x) {
    j.at("user_id").get_to(x.user_id);
    j.at("name").get_to(x.name);
    if (j.contains("nickname") && !j.at("nickname").is_null()) x.nickname = j.at("nickname").get<std::string>();
    j.at("score").get_to(x.score);
}
auto users = nlohmann::json::parse(text).get<std::vector<User>>();

Type mapping

JSONC++
"text"std::string
42int64_t
3.14double
truebool
[...]std::vector<T>
{...}struct
null or missingstd::optional<T>
unknownnlohmann::json

Tips

  • Errors: j.at() throws nlohmann::json::out_of_range when a required key is missing, so wrap parsing in try/catch.
  • Performance: for very large documents, consider the SAX interface or simdjson for read-only access.
  • Header-only: everything is inline, so the header can be included from multiple translation units.

Prefer Rust? Use JSON to Rust. Need a schema for gRPC? Try JSON to Protobuf.

How to use the JSON to C++

  1. Paste your data into the input box, upload a file, or click Sample to try it out.
  2. Adjust the options above the editor if you need to (indentation, delimiter, and so on).
  3. The C++ appears instantly in the output panel. Any errors are shown with their line and column.
  4. Click Copy to copy the result, or Download to save it as a file.

Frequently asked questions

Which JSON library does it target?

nlohmann/json (JSON for Modern C++), the most widely used C++ JSON library. Install it with vcpkg, Conan, your package manager or the single-header release.

How do I parse JSON into the structs?

auto user = nlohmann::json::parse(text).get<User>(); The generated from_json is found automatically through argument-dependent lookup.

Why are from_json and to_json written out instead of using a macro?

The NLOHMANN_DEFINE_TYPE macros require member names to match JSON keys and don't handle optional fields. Explicit functions support renamed keys and std::optional.

Which C++ standard is required?

C++17, for std::optional.

Is my JSON uploaded to a server?

No. All JSON tools run entirely in your browser, so your data never leaves your device.

Is there a size limit?

There is no fixed limit. Files of several megabytes work fine; very large files depend on your device's memory.

Do I need an account?

No. Every JSON tool is free and works without signing up.

Last updated:

Related JSON tools