Working with JSON in C++: Data Parsing and Serialization

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with JSON in C++: Parsing and Serialization



JSON (JavaScript Object Notation) remains one of the most popular formats for data exchange in web applications, configuration files, and APIs. Despite the fact that C++ does not have built-in JSON support, there are several powerful libraries that make working with this format simple and efficient. In this article, we will look at two of the most popular libraries: nlohmann/json (recommended for most projects) and RapidJSON (for tasks where performance is critical). You will learn how to parse JSON, serialize objects back to a string, and handle errors.



Why is JSON needed in C++?



C++ is a systems programming language, but modern applications increasingly require interaction with web services, storing configurations, or exchanging data between microservices. JSON provides a convenient, human-readable format for these tasks. Using libraries for working with JSON in C++ allows you to:

  • Load and process configuration files (for example, for game engines or servers).
  • Interact with REST APIs (sending and receiving data).
  • Save and restore the state of complex data structures.
  • Integrate with other languages and systems that use JSON.


nlohmann/json Library: Simplicity and Convenience



The nlohmann/json library (often called "JSON for Modern C++") is perhaps the most popular due to its intuitive API, reminiscent of working with Python or JavaScript. It supports C++11 and higher.



Installation

The simplest way is to copy the single header file json.hpp into your project. You can download it from the official repository or install it via a package manager (vcpkg, Conan).



Parsing JSON from a String

Let's look at a basic example of parsing a JSON string and accessing data:

#include <iostream>#include <nlohmann/json.hpp>using json = nlohmann::json;

int main() { // JSON string for parsing std::string json_string = R"({ "name": "C++", "version": 17, "features": ["RAII", "Templates", "Smart Pointers"], "is_compiled": true })";

// Parsing the string into a json object json data = json::parse(json_string);

// Accessing values std::cout << "Language: " << data["name"] << std::endl; std::cout << "Version: " << data["version"] << std::endl; std::cout << "Compiled: " << data["is_compiled"] << std::endl;

// Accessing an array for (auto& feature : data["features"]) { std::cout << " - " << feature << std::endl; } return 0;}

Output:

Language: "C++"Version: 17Compiled: true - "RAII" - "Templates" - "Smart Pointers"


Serialization (Creating JSON)

Creating a JSON object and converting it to a string is just as simple:

#include <iostream>#include <nlohmann/json.hpp>using json = nlohmann::json;

int main() { // Creating a JSON object json person; person["name"] = "Alice"; person["age"] = 30; person["is_student"] = false; person["hobbies"] = {"reading", "coding", "chess"};

// Adding a nested object person["address"]["city"] = "Moscow"; person["address"]["zip"] = "101000";

// Serialization to a string with indentation std::string serialized = person.dump(4); // 4 spaces for indentation std::cout << serialized << std::endl;

return 0;}

The dump(4) method returns a nicely formatted string. For compact output, use

Blogs

Book Recommendations