JSON Processing in C++: Libraries, Parsing and Examples
JSON (JavaScript Object Notation) is one of the most popular data exchange formats. Although C++ does not have built-in JSON support, there are several powerful libraries that allow you to easily parse, generate, and modify JSON data. In this article, we will look at three main libraries: nlohmann/json, RapidJSON, and Boost.PropertyTree, and provide practical examples of their use.
Why do you need a JSON library in C++?
C++ is a compiled language focused on performance. Manually parsing JSON using regular expressions or string operations is a labor-intensive and error-prone task. Specialized libraries provide:
- High parsing and serialization speed.
- An intuitive API (often in STL style).
- Support for JSON standards (including nested objects, arrays, null, Unicode).
- Minimal dependencies (often header-only).
1. nlohmann/json Library (JSON for Modern C++)
The most popular and easy-to-use library. Distributed as header-only (a single header file). Ideal for educational projects and small applications.
Installation
Download the json.hpp file from the nlohmann/json repository and include it:
#include "json.hpp"using json = nlohmann::json;Example: parsing JSON from a string
#include <iostream>#include "json.hpp"
int main() { std::string json_str = R"({ "name": "PythonLib", "year": 2025, "tags": ["C++", "JSON", "education"], "active": true })";
json data = json::parse(json_str);
std::cout << "Name: " << data["name"] << ""; std::cout << "Year: " << data["year"] << ""; std::cout << "First tag: " << data["tags"][0] << ""; std::cout << "Active: " << data["active"] << "";
return 0;}Example: creating and serializing JSON
#include <iostream>#include "json.hpp"
int main() { json obj; obj["title"] = "JSON in C++"; obj["pages"] = 42; obj["chapters"] = {"intro", "basics", "advanced"}; obj["metadata"]["author"] = "PythonLib Team";
std::string serialized = obj.dump(4); // 4 — indentation for pretty printing std::cout << serialized << "";
return 0;}2. RapidJSON Library
RapidJSON is a high-performance library focused on speed and minimal memory consumption. Often used in games, server applications, and embedded systems. Supports SAX-style (streaming parsing) and DOM-style.
Installation
Download the archive from GitHub and include the header files:
#include "rapidjson/document.h"#include "rapidjson/writer.h"#include "rapidjson/stringbuffer.h"Example: parsing and reading
#include <iostream>#include "rapidjson/document.h"
int main() { const char* json = "{\\"project\\":\\"PythonLib\\",\\"version\\":2.0}"; rapidjson::Document doc; doc.Parse(json);
if (doc.HasParseError()) { std::cerr << "Parse error!"; return 1; }
std::cout << "Project: " << doc["project"].GetString() << ""; std::cout << "Version: " << doc["version"].GetDouble() << "";
return 0;}Example: generating JSON
#include <iostream>#include "rapidjson/docume Also in library
Working with ZIP Archives in Kotlin: Creation, Reading, and Extraction
Prometheus metrics in C# (.NET) — prometheus-net library
Playwright for JavaScript (Node.js): Complete Guide with Examples
Sending SMS in C++: LibCurl API Library and REST Requests