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 libraries that make working with this format simple and convenient. In this article, we will look at how to parse and serialize JSON data in C++ using the popular nlohmann/json library. The material is aimed at beginner developers who are already familiar with the basics of C++.
1. Installing and Including the nlohmann/json Library
The nlohmann/json library is one of the most popular and easy-to-use libraries for working with JSON in C++. It supports C++11 and later, and works on all major platforms.
Installation Methods
- Download the header file: go to the GitHub repository and download the
json.hppfile. - Install via vcpkg:
vcpkg install nlohmann-json - Install via Conan:
conan install nlohmann_json/3.11.2@
After installation, simply include the file in your project:
#include <iostream>#include "json.hpp"using json = nlohmann::json;
int main() { std::cout << "Library included!" << std::endl; return 0;}2. JSON Parsing: Reading Data from a String and File
JSON parsing is the process of converting a JSON-formatted string into C++ data structures. The nlohmann/json library makes this very simple.
Parsing from a String
Let's look at an example of parsing a simple JSON object:
#include <iostream>#include "json.hpp"using json = nlohmann::json;
int main() { std::string json_str = R"({ "name": "Anna", "age": 25, "is_student": false, "hobbies": ["reading", "swimming", "programming"] })";
// Parsing the string json data = json::parse(json_str);
// Accessing data std::string name = data["name"]; int age = data["age"]; bool is_student = data["is_student"]; std::vector<std::string> hobbies = data["hobbies"];
std::cout << "Name: " << name << std::endl; std::cout << "Age: " << age << std::endl; std::cout << "Student: " << (is_student ? "yes" : "no") << std::endl; std::cout << "Hobbies: "; for (const auto& hobby : hobbies) { std::cout << hobby << " "; } std::cout << std::endl;
return 0;}Parsing from a File
Often JSON data is stored in files. Here's how to read it:
#include <iostream>#include <fstream>#include "json.hpp"using json = nlohmann::json;
int main() { std::ifstream file("data.json"); if (!file.is_open()) { std::cerr << "Error opening file!" << std::endl; return 1; }
json data = json::parse(file); file.close();
// Outputting data std::cout << "Data from file: " << data.dump(4) << std::endl; return 0;}The dump(4) method converts JSON back to a string with 4-space indentation for easy reading.
3. Serialization: Creating JSON from C++ Data
Serialization is the reverse process: converting C++ data into a JSON string. The library allows you to create JSON objects in an intuitive way.
Creating a Simple JSON Object
#include <iostream>#include "json.hpp"using json = nlohmann::json;
int main() { json person;
person["name"] = "Ivan";