TopGit
GitHub Repo Review

nlohmann/json C++ library: JSON for Modern C++

NTopGit review image for nlohmann/json
Review by Topgit.dev for nlohmann/json, with GitHub repository stats and README context.
Quick verdict

nlohmann/json is a single-header C++ library that turns JSON parsing and serialization into ordinary operator[] and initializer-list code instead of a DOM API you have to learn. Reach for it when you want JSON to feel like a built-in C++ type and you're fine trading some raw speed for that convenience. Skip it if you're parsing JSON in a tight, high-throughput loop — the project's design goals say speed wasn't the priority.

Stars
★ 50.9k
Forks
⑂ 7.5k
Language
C++
License
MIT
Topic
—
Updated
Nov 2026
Homepage
GitHub

What is nlohmann/json

nlohmann/json is a single-file C++ header (json.hpp) that gives the language a native-feeling JSON value type, released under the MIT license. It maps JSON's types onto familiar C++ containers — std::map, std::vector, std::string — so JSON reads like the STL instead of a bespoke API, and it can also read and write binary formats like BSON, CBOR, MessagePack, and UBJSON.

Key features of nlohmann/json

  • ✓Header-only — just json.hpp; #include it, no build system changes, no linked library.
  • ✓STL-style API: operator[], initializer lists, iterators, and range-based for loops work the way they do on std::map and std::vector, since the library maps JSON onto ordinary STL containers.
  • ✓JSON Pointer (RFC 6901) and JSON Patch (RFC 6902) support, plus JSON Merge Patch, for addressing and diffing values inside a document.
  • ✓Reads and writes non-text formats too — BSON, CBOR, MessagePack, and UBJSON/BJData — through the same basic_json class used for plain JSON.
  • ✓SAX-style parsing via sax_parse(), for streaming large documents without building the whole DOM in memory.
  • ✓Heavily unit-tested with 100% code coverage per the README, plus continuous fuzzing through Google OSS-Fuzz.
  • ✓A user-defined _json string literal suffix for parsing a raw JSON string into a json object inline.
  • ✓Optional C++20 modules support — build with NLOHMANN_JSON_BUILD_MODULES and swap #include for import nlohmann.json.
How this repository's GitHub stars have grown over time. Source: star-history.com.View the star history ↗

When to use nlohmann/json

  • •Parsing and writing config files in a C++ application without hand-rolling a parser or pulling in a schema-driven library.
  • •Building and consuming REST API payloads in a C++ client or service — construct a json object, dump() it, send it.
  • •Converting between JSON and compact binary wire formats (CBOR, MessagePack) for network transport or storage.
  • •Diffing and patching JSON documents with the built-in JSON Patch and JSON Merge Patch functions.
  • •Prototyping quickly in vanilla C++11 code where adding a full build dependency isn't worth it yet.

Using nlohmann/json in your C++ project

Include the single header and alias the type: `#include <nlohmann/json.hpp>` and `using json = nlohmann::json;`. Parse JSON from a file or string with `json::parse()`: ```cpp std::ifstream f("example.json"); json data = json::parse(f); ``` Build values with plain assignment or initializer lists — the class infers the JSON type from the C++ type you assign: ```cpp json j; j["pi"] = 3.141; j["happy"] = true; j["list"] = {1, 0, 2}; ``` Serialize back to text with `.dump()`, optionally passing an indent width for pretty-printing: `j.dump(4)`. Streams work too — `std::cout << j;` writes compact JSON, and `std::cout << std::setw(4) << j;` pretty-prints it.

Strengths

  • ✓Drop-in integration. One header, no build system wiring, no linked library to manage.
  • ✓API reads like the STL, so C++ developers are productive with it in minutes, not hours.
  • ✓Extensive test coverage and continuous OSS-Fuzz fuzzing, per the README, catch parser edge cases most hand-rolled parsers miss.
  • ✓Speaks CBOR, MessagePack, BSON, and UBJSON out of the box, not just text JSON, for storage or wire transport.
  • ✓MIT license — no copyleft concerns embedding it in closed-source products.

Limitations and known trade-offs

  • △Memory efficiency wasn't a design priority: the README says every JSON object carries the overhead of one pointer plus one enum byte, which adds up on large in-memory documents.
  • △Not built for raw speed — the project's own design goals note there are faster JSON libraries out there, and picking this one is a trade for convenience, not benchmark wins.
  • △UTF-8 encoding only — dump() throws on invalid UTF-8 unless you explicitly set an error_handler to replace or ignore bad bytes.
  • △Needs at least vanilla C++11 to compile, so it's not an option for projects stuck on an older standard.

Alternatives to nlohmann/json

RapidJSON — a header-based C++ JSON library built for raw parsing speed with a lower-level SAX/DOM API, at the cost of the ergonomic operator[] syntax.simdjson — a SIMD-accelerated parser aimed at projects where parsing throughput is the bottleneck.jsoncpp — an older, widely-packaged C++ JSON library with a more verbose, class-based API.

Frequently asked questions

Can I use nlohmann/json commercially?

nlohmann/json ships under the MIT license, so commercial use, modification, and redistribution are all permitted without royalty payments or copyleft obligations. Companies can embed the single json.hpp header directly into closed-source codebases, since MIT only requires keeping the copyright notice intact.

How does nlohmann/json compare to RapidJSON?

RapidJSON focuses on raw parsing and serialization speed with a lower-level SAX/DOM API. nlohmann/json's own design goals explicitly say memory efficiency and speed were not top priorities — it optimizes for STL-like, intuitive syntax instead. Pick RapidJSON for a speed-critical hot path; pick nlohmann/json when developer ergonomics and a single header matter more.

Does nlohmann/json support null values and optional fields?

nlohmann/json represents JSON null natively — assigning nullptr to a json value creates a null node, and is_null() checks for it. There's no dedicated 'optional field' wrapper; missing keys are typically handled with value(key, default) or by checking contains() before calling at().

What character encodings does nlohmann/json support?

nlohmann/json only supports UTF-8 internally. Strings in other encodings can still be stored in a json value, but calling dump() throws an exception on invalid UTF-8 unless you pass json::error_handler_t::replace or ::ignore to swap or drop the offending bytes.

How fast is nlohmann/json for parsing and serialization?

nlohmann/json's README doesn't publish benchmark numbers — it states plainly that speed wasn't a design priority and that faster JSON libraries exist. The trade-off is deliberate: you get single-header simplicity and STL-like syntax instead of the fastest possible parse times, which matters most if JSON parsing sits in a hot loop.

Can I serialize custom C++ types with nlohmann/json?

nlohmann/json's API reference lists get<T>() and an implicit conversion operator for pulling arbitrary C++ types out of a json value, the template-based mechanism the library relies on for custom types. The exact steps for teaching it about your own struct live on the project's documentation site rather than in the README.

The problem it solves

C++ has no built-in JSON type — the standard library gives you streams and containers, but nothing that parses or emits JSON, so projects either hand-roll a parser, wire up a schema-driven serialization framework for a job that doesn't need one, or resort to string concatenation for config files and small API payloads. nlohmann/json exists to close that specific gap without asking for a build step.

How to install / try

The primary path is copying the single header: grab `single_include/nlohmann/json.hpp` from the repo and `#include` it — no library to link, no CMake changes required. The README also says the library is available through popular package managers, with the specifics documented on its integration page rather than in the README itself.

Who should try it — and who should skip

Try nlohmann/json if you're writing C++ and want JSON handling that reads like normal container code — config parsing, small API clients, tooling scripts, anywhere you'd rather write j["key"] than wrestle a DOM API. Skip it if you're building a high-throughput parser on the hot path of a service, where every allocation counts and a SIMD-first library like simdjson or a speed-tuned one like RapidJSON will serve you better; skip it too if your toolchain can't reach C++11.

Related repositories

Source & attribution

Based on the nlohmann/json GitHub repository (https://github.com/nlohmann/json) and its README.

GitHub data · last synced Aug 14, 2026Reviewed by Henry
← Back to TopGit

Want a second opinion on json?

Ask an AI that can read this page — one click and you get its take on json.

GitHub