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

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 in Practice



JSON (JavaScript Object Notation) is one of the most popular data interchange formats in modern applications. Despite the fact that C++ does not have built-in JSON support, there are many libraries that simplify working with this format. In this article, we will look at practical aspects of parsing and serializing JSON in C++ using the nlohmann/json library — one of the most popular and convenient to use.



Why choose nlohmann/json?



The nlohmann/json library (often called JSON for Modern C++) has gained popularity due to its intuitive API, reminiscent of working with STL containers. It is header-only (requires no compilation), supports C++11 and later, and provides high performance and type safety.



Installing the library is simple: just download one header file json.hpp or use a package manager like vcpkg, Conan, or CMake FetchContent.



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


Basics of JSON Parsing in C++



JSON parsing is the process of converting a string into structured data. The nlohmann/json library provides several ways to parse.



Parsing from a String



The most common way is using the static method json::parse():



#include <iostream>#include <string>#include <nlohmann/json.hpp>

int main() { std::string json_string = R"({ "name": "Ivan", "age": 30, "is_student": false, "skills": ["C++", "Python", "JavaScript"], "address": { "city": "Moscow", "zip": "101000" } })";

try { json data = json::parse(json_string); // Accessing data std::cout << "Name: " << data["name"] << std::endl; std::cout << "Age: " << data["age"] << std::endl; std::cout << "Student: " << (data["is_student"] ? "yes" : "no") << std::endl; // Working with arrays for (auto& skill : data["skills"]) { std::cout << "Skill: " << skill << std::endl; } // Working with nested objects std::cout << "City: " << data["address"]["city"] << std::endl; } catch (json::parse_error& e) { std::cerr << "Parse error: " << e.what() << std::endl; } return 0;}


Parsing from a File



For working with files, use C++ I/O streams:



#include <fstream>#include <nlohmann/json.hpp>

int main() { std::ifstream file("data.json"); if (!file.is_open()) { std::cerr << "Failed to open file" << std::endl; return 1; } try { json data = json::parse(file); std::cout << "Data loaded: " << data.dump(2) << std::endl; } catch (json::parse_error& e) { std::cerr << "Error: " << e.what() << std::endl; } return 0;}


JSON Serialization in C++



Serialization is the reverse process: converting C++ data into a JSON string. The library offers the dump() method with formatting options.



Creating a JSON Object



You can create JSON objects directly, like regular associative arrays:



#include <nlohmann/json.hpp>#include <iostream>

int main() { json person; person["name"] = "Maria"; person["age"] = 25; person["is_employed"] = true; person["languages"] = {"Russian", "English", "German"};

Blogs

Book Recommendations