Introduction to Working with JSON in C++
JSON (JavaScript Object Notation) is a lightweight data interchange format widely used in web development, configuration files, and APIs. Although C++ does not have built-in JSON support, there are many reliable libraries that simplify parsing and serialization of data. In this article, we will look at two popular libraries: nlohmann/json (recommended for beginners and modern projects) and Boost.PropertyTree (for those already using Boost). You will learn how to read, create, modify, and output JSON data in C++.
nlohmann/json Library: Installation and Basic Principles
The nlohmann/json library (often called "JSON for Modern C++") is one of the most popular due to its simplicity and intuitive API. It supports C++11 and later, has no external dependencies, and is distributed as a single header file json.hpp.
Installation
Download the json.hpp file from the GitHub repository and place it in your project folder. Then include it in your code:
#include <iostream>#include <fstream>#include "json.hpp" // Include the library
using json = nlohmann::json;Parsing JSON from String and File
The library allows parsing JSON from a string, file, or input stream. Let's look at an example of parsing from a string:
#include <iostream>#include "json.hpp"
using json = nlohmann::json;
int main() { // JSON string std::string jsonString = R"({ "name": "Alice", "age": 30, "isStudent": false, "hobbies": ["reading", "coding"] })"; // Parsing json data = json::parse(jsonString); // Accessing data std::cout << "Name: " << data["name"] << std::endl; std::cout << "Age: " << data["age"] << std::endl; std::cout << "Is student: " << (data["isStudent"] ? "true" : "false") << std::endl; // Iterating over an array for (auto& hobby : data["hobbies"]) { std::cout << "Hobby: " << hobby << std::endl; } return 0;}To parse from a file, use json::parse(std::ifstream("data.json"));.
JSON Serialization
Creating a JSON object and serializing it to a string is done naturally:
#include <iostream>#include "json.hpp"
using json = nlohmann::json;
int main() { json person; person["name"] = "Bob"; person["age"] = 25; person["isStudent"] = true; person["hobbies"] = {"gaming", "swimming"}; // Nested object json address; address["city"] = "Moscow"; address["street"] = "Tverskaya"; person["address"] = address; // Serialization to string with indentation std::string serialized = person.dump(4); // 4 spaces for formatting std::cout << serialized << std::endl; // Saving to file std::ofstream file("output.json"); file << person.dump(2); file.close(); return 0;}The dump() method converts the object to a string. The parameter specifies the number of spaces for indentation (0 for compact output).
Boost.PropertyTree Library: An Alternative Approach
If your project already uses Boost, the Boost.PropertyTree library provides a convenient way to work with JSON and other formats (XML, INI). It is less specialized for JSON but integrates well with the Boost ecosystem.
Parsing JSON with Boost.PropertyTree
#include <iostream>#include <boost/property_tree/ptree.hpp>\