opensourceprojects.dev

A broadsheet for software that doesn't ask for your email

JSON for Modern C++: single-header, zero-dependency parsing with STL-like access
GitHub RepoImpressions2

Project Description

View on GitHub

JSON for Modern C++: The Single-Header Library That Makes JSON Feel Native

You've probably been there: you need to parse a JSON file in C++, and suddenly you're knee-deep in a dependency chain, wrestling with build systems, or writing boilerplate just to read a simple key-value pair. It's a frustrating experience that makes you wonder why something so common has to be so painful. That's exactly the problem JSON for Modern C++ sets out to solve, and it does so with a remarkably simple approach: one header file, zero dependencies, and an API that feels like it was designed by someone who actually uses C++ every day.

What It Does

JSON for Modern C++ (also known as nlohmann/json) is a library that brings JSON parsing and serialization to C++11 and beyond. The core idea is that JSON should feel like a first-class citizen in your code, not an awkward bolt-on. It achieves this through a design that's both pragmatic and elegant.

The library is a single header file—you literally drop json.hpp into your project and you're done. No linking, no configuration, no package manager gymnastics. It's written in modern C++ and provides a json class that acts as a container for any JSON value: objects, arrays, strings, numbers, booleans, and null. Under the hood, it uses STL containers, which means it plays nicely with everything you already know.

The project is actively maintained, with continuous integration across Ubuntu, macOS, and Windows, plus a robust testing and fuzzing pipeline. It's also MIT-licensed, so you can use it in commercial projects without legal headaches. The documentation is extensive, and there's a live online playground where you can experiment before you even download anything.

Why It's Cool

What makes this library genuinely enjoyable to use isn't just that it works—it's how it works. Here's what stands out:

  • Zero-dependency, single-header design. You can't overstate how refreshing this is. The entire library lives in one file. You copy it into your project, #include "json.hpp", and you're done. No CMake modules, no vcpkg or Conan setup, no symbol collisions. It just works.

  • STL-like access means no learning curve. If you know how to use std::map and std::vector, you already know how to use this library. You access JSON object members with [] or .at(), iterate with range-based for loops, and use familiar methods like .size() and .empty(). It feels like working with a standard container, because in a sense, you are.

  • JSON literals are first-class citizens. You can write json j = R"({"name": "Alice", "age": 30})"_json; and get a parsed object. That's not a typo—the library provides a user-defined literal that lets you embed JSON directly in your source code. It's a small touch, but it makes testing and prototyping dramatically more pleasant.

  • Seamless conversion from STL containers. If you have a std::vector<int> or a std::map<std::string, double>, you can construct a json object directly from it. The library handles the conversion automatically. This means your existing data structures don't need to be restructured to work with JSON—they just work.

  • Batteries included for serialization. The library handles both parsing (deserialization) and pretty-printing (serialization) out of the box. You can dump a json object to a string with .dump(), and you can control indentation, key ordering, and error handling. It covers the full round-trip without extra tooling.

  • Serious engineering behind it. The project has continuous integration on three major OSes, code coverage tracking, static analysis, and even fuzzing through OSS-Fuzz. There's a Discord community and active issue resolution. This isn't a weekend hobby project—it's a library people rely on in production.

How to Try It

Getting started is almost absurdly easy. First, grab the header file from the releases page or clone the repository:

git clone https://github.com/nlohmann/json.git

Then, copy the single header file into your project:

cp json/single_include/nlohmann/json.hpp your_project/

Now you can start using it. Here's a minimal example that reads a JSON file and accesses a value:

#include "json.hpp"
#include <fstream>
#include <iostream>

using json = nlohmann::json;

int main() {
    std::ifstream file("data.json");
    json data = json::parse(file);

    std::cout << "Name: " << data["name"] << std::endl;
    std::cout << "Age: " << data["age"] << std::endl;
    return 0;
}

Or, if you want to create JSON objects from literals without parsing a file:

using json = nlohmann::json;

// Using the _json user-defined literal
json j = R"({
    "name": "Alice",
    "age": 30,
    "hobbies": ["reading", "hiking"]
})"_json;

// Access like an STL container
std::cout << j["hobbies"][0] << std::endl;  // prints "reading"

You can also construct objects directly from STL containers:

std::vector<int> numbers = {1, 2, 3, 4, 5};
json j(numbers);  // becomes [1, 2, 3, 4, 5]

The documentation is thorough and well-organized, with a dedicated FAQ section and a full API reference. There's also a live playground where you can test code in your browser before committing to anything.

Final Thoughts

JSON for Modern C++ is one of those libraries that makes you wonder why it took so long to exist. It's not flashy—there's no magic, no clever trickery—just solid engineering that removes friction. If you work with JSON in C++ and you're tired of fighting your tooling, this is worth your time. It's particularly great for smaller projects where pulling in a heavyweight framework feels like overkill, but it scales fine for larger codebases too.

The single-header approach might not be for everyone—some people prefer modular dependencies and explicit versioning—but for most developers, the simplicity is a feature, not a limitation. Give it a spin on your next project; you might find that JSON stops being a chore and starts being something you don't think about at all.

Follow @githubprojects for more developer tools and open source projects.

Back to Projects
Project ID: 86c46c1b-6210-498e-b431-e41257edb103Last updated: August 23, 2026 at 02:43 AM