JSON Processing in C++: Library Overview and Practical Examples
JSON (JavaScript Object Notation) is a lightweight data interchange format widely used in web applications, APIs, and configuration files. In C++, JSON handling is not built into the standard library, so developers use third-party libraries. In this article, we will look at three of the most popular tools: nlohmann/json, RapidJSON, and Boost.JSON. You will learn how to install them, parse JSON, create your own structures, and serialize data. All examples are accompanied by C++ code.
1. The nlohmann/json Library
nlohmann/json is the most popular library for working with JSON in C++. It is known for its simple and intuitive syntax, reminiscent of Python or JavaScript. The library is header-only, which simplifies integration: you just need to copy the json.hpp file into your project.
Installation
Download json.hpp from GitHub and include it:
#include "json.hpp"using json = nlohmann::json;Parsing a JSON String
#include <iostream>#include "json.hpp"
int main() { std::string json_str = R"({ "name": "Alice", "age": 30, "is_student": false, "skills": ["C++", "Python", "JSON"] })";
json data = json::parse(json_str);
std::cout << "Name: " << data["name"] << std::endl; std::cout << "Age: " << data["age"] << std::endl; std::cout << "First skill: " << data["skills"][0] << std::endl;
return 0;}Creating and Serializing JSON
json person;person["name"] = "Bob";person["age"] = 25;person["is_student"] = true;person["languages"].push_back("C");person["languages"].push_back("Java");
std::string serialized = person.dump(4); // 4 — indentation for pretty printingstd::cout << serialized << std::endl;2. The RapidJSON Library
RapidJSON is a high-performance library focused on speed and minimal memory usage. It is also header-only and supports both SAX-style (streaming) and DOM-style (tree-building) parsing. It is suitable for projects where performance is critical, such as game engines or high-load servers.
Installation
Download the files from the RapidJSON repository and include them:
#include "rapidjson/document.h"#include "rapidjson/writer.h"#include "rapidjson/stringbuffer.h"Parsing JSON
#include <iostream>#include "rapidjson/document.h"
int main() { const char* json = "{\\"name\\":\\"Charlie\\",\\"age\\":28,\\"active\\":true}"; rapidjson::Document doc; doc.Parse(json);
if (doc.HasParseError()) { std::cerr << "Parse error!" << std::endl; return 1; }
std::cout << "Name: " << doc["name"].GetString() << std::endl; std::cout << "Age: " << doc["age"].GetInt() << std::endl;
return 0;}Creating JSON and Serialization
rapidjson::Document doc;doc.SetObject();rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
doc.AddMember("name", "Diana", allocator);doc.AddMember("score", 95.5, allocator);
rapidjson::StringBuffer buffer;rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);doc.Accept(writer);
std::cout << buffer.GetString() << std::endl;3. The Boost.JSON Library
Boost.JSON is part of the Boost libraries, officially accepted into C++ Bo