JSON to C++: Structs You Can Read with nlohmann/json
Paste a JSON response or config file and get C++17 structs, each with from_json and to_json, ready to drop next to json.hpp.
Paste or upload your JSON
Start with any JSON object, or a list of objects such as an API response. If the JSON was pasted from a log as one quoted string, it is unwrapped first.
Example input
{
"product": {
"id": 12345,
"name": "Smart Watch Pro",
"price": 299.99,
"inStock": true,
"tags": ["electronics", "wearable", "fitness"],
"specifications": {
"battery": "48 hours",
"waterproof": true,
"screenSize": 1.4
}
}
}Read the generated structs
Every object turns into a struct with public members, and text, lists and nested objects map onto std::string, std::vector and your own struct types.
Check the types
This is the full output for the example above. The structs use the arbitrary types feature of nlohmann/json, so get<Root>() and assigning a Root to a json value both work without extra code.
C++ output
// Generated from your JSON. Needs C++17 and nlohmann/json (json.hpp).
// Read it with:
// Root data = nlohmann::json::parse(text).get<Root>();
// nlohmann::json back = data; // write it out again with back.dump(2)
#pragma once
#include <cstdint>
#include <optional>
#include <string>
#include <vector>
#include "nlohmann/json.hpp"
struct Specifications {
std::string battery{};
bool waterproof{};
double screenSize{};
};
inline void from_json(const nlohmann::json& j, Specifications& x) {
j.at("battery").get_to(x.battery);
j.at("waterproof").get_to(x.waterproof);
j.at("screenSize").get_to(x.screenSize);
}
inline void to_json(nlohmann::json& j, const Specifications& x) {
j = nlohmann::json::object();
j["battery"] = x.battery;
j["waterproof"] = x.waterproof;
j["screenSize"] = x.screenSize;
}
struct Product {
int id{};
std::string name{};
double price{};
bool inStock{};
std::vector<std::string> tags{};
Specifications specifications{};
};
inline void from_json(const nlohmann::json& j, Product& x) {
j.at("id").get_to(x.id);
j.at("name").get_to(x.name);
j.at("price").get_to(x.price);
j.at("inStock").get_to(x.inStock);
j.at("tags").get_to(x.tags);
j.at("specifications").get_to(x.specifications);
}
inline void to_json(nlohmann::json& j, const Product& x) {
j = nlohmann::json::object();
j["id"] = x.id;
j["name"] = x.name;
j["price"] = x.price;
j["inStock"] = x.inStock;
j["tags"] = x.tags;
j["specifications"] = x.specifications;
}
struct Root {
Product product{};
};
inline void from_json(const nlohmann::json& j, Root& x) {
j.at("product").get_to(x.product);
}
inline void to_json(nlohmann::json& j, const Root& x) {
j = nlohmann::json::object();
j["product"] = x.product;
}
Copy or download the header
The download is a .hpp header with #pragma once. Get json.hpp from the nlohmann/json releases, or install it with vcpkg (nlohmann-json), Conan (nlohmann_json) or CMake FetchContent.
#include <fstream>
#include <iostream>
#include "converted_data.hpp"
int main() {
std::ifstream file("product.json");
Root data = nlohmann::json::parse(file).get<Root>();
data.product.price = 279.99;
nlohmann::json back = data;
std::cout << back.dump(2) << std::endl;
}What does JSON to C++ conversion give you?
Reading JSON in C++ usually means either walking an untyped json value by hand or writing a struct and its conversion functions yourself. This page writes that second part for you: one struct per JSON object, with member types picked from your actual data, plus the from_json and to_json pair that nlohmann/json looks for.
It is a starting point, not a schema: the types only reflect the sample you paste. If a field can be missing in real data but is present in your sample, change it to std::optional by hand. Working in another language? Try JSON to C# or JSON to C, and check odd input first with the JSON validator.
Frequently Asked Questions
Which JSON library does the generated code use?
nlohmann/json, the single-header json.hpp. Each struct gets free from_json and to_json functions that the library finds on its own, so nlohmann::json::parse(text).get<Root>() fills a Root and nlohmann::json j = root; turns it back into JSON. We compile the output against versions 3.11 and 3.12 of the library.
Which C++ standard do I need?
C++17. The code uses std::optional and if statements with an initializer, both from C++17. It is plain standard C++ with no compiler extensions, and our test builds use g++ -std=c++17 -Wall with warnings treated as errors.
How does it choose int, int64_t, uint64_t or double?
It reads how each number is written. Whole numbers that fit in 32 bits become int, bigger ones up to 9223372036854775807 become int64_t, so an ID like 8901240544102066246 keeps every digit, and positive values up to 18446744073709551615 become uint64_t. Numbers with a decimal point or exponent, like 14.50, 1.0 or 1e3, become double, and a list that mixes 1 and 2.5 becomes std::vector<double>. Quoted values such as "00501" stay std::string. A double does not remember how it was written, so 14.50 comes back out as 14.5.
What happens when list items have different fields?
Every item in a list is checked, not just the first. A field that only some items have, or that is null in some of them, becomes std::optional<T>. A field that holds a number in one item and text in another, or is always null, becomes nlohmann::json so nothing is lost. Lists with null items become std::vector<std::optional<T>>, and a short adl_serializer for std::optional is added so older versions of nlohmann/json can read them.
What does it do with keys that are not valid C++ names?
A key that is already a valid identifier is used as is. Keywords and names that clash with standard types or macros, such as class, int, NULL or errno, get a trailing underscore. Dashes and spaces turn into camelCase (first-name becomes firstName), keys that start with a digit get a field prefix, and names that end up the same are numbered. from_json and to_json always use your original key, so the data still lines up.
Can the top level of my JSON be an array?
Yes. If the array holds objects, Root describes one item and the comment at the top shows how to read the whole file with get<std::vector<Root>>(). A JSON file that is just a string, a number or a list of plain values has no object to describe, so you get a short note instead of a struct.
Is my JSON sent anywhere?
No. The structs are generated in your browser. Nothing you paste or upload is sent to a server or kept, and a reload starts with an empty editor.
Related Tools
JSON to C
Convert JSON data to C structs with parsing functions and memory management
JSON to C#
Convert JSON data to C# classes with properties and attributes
JSON to Python
Convert JSON data to Python dataclasses with type hints
JSON to Java
Convert JSON data to Java classes with getters, setters, and annotations
JSON to TypeScript
Convert JSON data to TypeScript interfaces and types
JSON to TS
Generate TS (TypeScript) type definitions from JSON data