JSON processing in C++ with the nlohmann/json library
In modern C++ development, working with JSON (JavaScript Object Notation) has become an integral part of creating web services, configuration files, and data exchange between systems. One of the most popular and convenient libraries for this task is nlohmann/json (or simply JSON for Modern C++). It offers an intuitive API, minimalistic syntax, and high performance. In this article, we will thoroughly examine how to use this library for parsing, creating, modifying, and serializing JSON data in C++.
Installation and setup
The nlohmann/json library is header-only (consists only of header files). To install it, simply download one file json.hpp from the official repository and include it in your project:
#include <nlohmann/json.hpp>using json = nlohmann::json;If you use CMake, you can include the library via FetchContent or vcpkg. Example for CMake:
FetchContent_Declare(json GIT_REPOSITORY https://github.com/nlohmann/json.git GIT_TAG v3.11.2)FetchContent_MakeAvailable(json)
target_link_libraries(your_project PRIVATE nlohmann_json::nlohmann_json)Main features of the library
- Parsing JSON from a string or file – converting text into structured data.
- Creating JSON objects – dynamically forming structures using operators
[]and methods. - Data access – reading values by keys and indices with type checking.
- Serialization – converting a JSON object back to a string with formatting.
- Working with arrays and nested objects – support for complex hierarchical structures.
- Error handling – exceptions for invalid data or types.
Example 1: Parsing and reading data
Let's examine a simple example: we have a JSON string containing information about a user. We parse it and extract the values:
#include <iostream>#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() { std::string json_str = R"( { "name": "Ivan", "age": 30, "is_student": false, "hobbies": ["reading", "programming", "sports"] } )";
// Parsing the string into a json object json user = json::parse(json_str);
// Accessing data with type checking std::string name = user["name"]; int age = user["age"]; bool is_student = user["is_student"];
std::cout << "Name: " << name << std::endl; std::cout << "Age: " << age << std::endl; std::cout << "Student: " << (is_student ? "yes" : "no") << std::endl;
// Working with an array auto& hobbies = user["hobbies"]; std::cout << "Hobbies: "; for (const auto& hobby : hobbies) { std::cout << hobby << " "; } std::cout << std::endl;
return 0;}Example 2: Creating and serializing JSON
Often it is necessary to form a JSON structure programmatically. The following code creates an object with book data and outputs it in a formatted form:
#include <iostream>#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() { // Create an empty object json book;
// Fill in the data book["title"] = "War and Peace"; book["author"] = "Leo Tolstoy"; book["year"] = 1869; book["genres"] = {"novel", "historical literature"};
// Nested object book["publisher"]["name"] = "Eksmo"; Also in library
QR Code Generation in C++: QR-Code-generator Library
Integration of Prometheus Metrics in Java: simpleclient Library
Parsing HTML in C# (.NET): HtmlAgilityPack and AngleSharp Libraries
neverthrow for TypeScript: Complete Guide with Examples