Introduction to nlohmann/json: What It Is and Why You Need It
The nlohmann/json library (also known as JSON for Modern C++) is one of the most popular and convenient libraries for working with JSON in the C++ language. It was developed by Niels Lohmann and is distributed under the MIT license. The main goal of the library is to provide an intuitive, modern, and safe interface for parsing, creating, modifying, and serializing JSON data.
Why is it needed? In modern development, JSON (JavaScript Object Notation) is the de facto standard for data exchange between services, storing configurations, working with APIs, and web applications. The standard C++ library does not have built-in JSON support, so developers have to either write their own parsers or use third-party solutions. nlohmann/json solves this problem by offering:
- Simple syntax — working with JSON resembles working with built-in STL containers (std::map, std::vector).
- Type safety — automatic conversion between C++ and JSON types.
- High performance — the library is optimized for speed and memory.
- Cross-platform — works on Windows, Linux, macOS, as well as embedded systems.
- Full JSON standard support — including nested objects, arrays, null, boolean values, numbers, and strings.
The library is actively used in industrial projects such as game engines, web servers, data analysis tools, and IoT devices.
Installing nlohmann/json
Unlike Python (where pip is used), installing libraries in C++ can be done in several ways. Let's look at the most popular ones.
Method 1: Header-only (recommended for beginners)
The nlohmann/json library is header-only — this means that to use it, you simply need to download one header file json.hpp and include it in your project. No compilation, linking, or dependency installation is required.
- Download the
json.hppfile from the official repository (latest version is v3.11.3). - Place it in your project folder, for example,
include/json.hpp. - In your source code, include:
#include "json.hpp".
Method 2: Installation via package managers
If you use modern build systems, you can install the library via a package manager:
- vcpkg (Windows, Linux, macOS):
vcpkg install nlohmann-json - Conan:
conan install nlohmann_json/3.11.3@ - Homebrew (macOS):
brew install nlohmann-json - APT (Ubuntu/Debian):
sudo apt install nlohmann-json3-dev
Method 3: CMake FetchContent
For CMake projects, you can automatically download the library during build:
include(FetchContent)FetchContent_Declare( json GIT_REPOSITORY https://github.com/nlohmann/json.git GIT_TAG v3.11.3)FetchContent_MakeAvailable(json)target_link_libraries(your_target PRIVATE nlohmann_json::nlohmann_json)Quick Start: First Example
Let's write a simple program that creates a JSON object, prints it to the screen, and then parses a JSON string back.
#include <iostream>#include "json.hpp"
using json = nlohmann::json;
int main() { // 1. Creating a JSON object json person = { {"name", "Alice"}, {"age", 30}, {"is_student", false}, {"skills", {"C++", "Python", "JSON"}},