Working with JSON in C++: Parsing and Serialization for Beginners

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

Introduction: Why is JSON needed in C++?

\n

JSON (JavaScript Object Notation) is a lightweight data interchange format widely used in web applications, configuration files, and APIs. Despite JSON originally appearing in JavaScript, today it has become the de facto standard for transmitting structured data between a server and a client in any programming language, including C++.

\n

In this article, we will cover how to work with JSON in C++: parsing (reading) and serializing (creating) JSON data. The material is aimed at beginner developers who are already familiar with the basics of C++ (variables, functions, classes) but have not yet worked with JSON.

\n

To work with JSON in C++, we will use the popular library nlohmann/json (also known as JSON for Modern C++). It is easy to install, intuitive, and great for learning.

\n\n

Installing the nlohmann/json Library

\n

Before writing code, you need to include the library. The simplest way is to use the header-only version (only one header file is needed).

\n\n

Method 1: Download the json.hpp file

\n
    \n
  • Go to the nlohmann/json GitHub repository.
  • \n
  • Download the json.hpp file from the latest release.
  • \n
  • Place it in your project folder.
  • \n
  • Include it in your code: #include "json.hpp"
  • \n
\n\n

Method 2: Use a package manager (vcpkg / Conan)

\n

If you use CMake, the easiest way is to install the library via vcpkg:

\n
vcpkg install nlohmann-json
\n

Or via Conan:

\n
conan install nlohmann_json/3.11.2@
\n\n

After installation, include the library in your code:

\n
#include <nlohmann/json.hpp>\nusing json = nlohmann::json;
\n\n

Now you are ready to go!

\n\n

Parsing JSON in C++: Reading Data

\n

Parsing is the process of converting a JSON string into an object that can be worked with in code. The nlohmann/json library makes this extremely simple.

\n\n

Example 1: Parsing a JSON string

\n

Suppose we have a JSON string containing information about a user:

\n
{\n  "name": "Alexey",\n  "age": 30,\n  "is_student": false,\n  "hobbies": ["reading", "programming", "cycling"]\n}
\n\n

Parsing is done in one line:

\n
#include <iostream>\n#include <nlohmann/json.hpp>\n\nusing json = nlohmann::json;\n\nint main() {\n    // Original JSON string\n    std::string json_str = R"(\n    {\n        "name": "Alexey",\n        "age": 30,\n        "is_student": false,\n        "hobbies": ["reading", "programming", "cycling"]\n    }\n    )";\n\n    // Parsing\n    json data = json::parse(json_str);\n\n    // Accessing data\n    std::string name = data["name"];\n    int age = data["age"];\n    bool is_student = data["is_student"];\n    std::vector<std::string> hobbies = data["hobbies"];\n\n    // Output\n    std::cout << "Name: " << name << std::endl;\n    std::cout << "Age: " << age << std::endl;\n    std::cout << "Student? " << (is_student ? "yes" : "no") << std::endl;\n    std::cout << "Hobbies: ";\n    for (const auto& hobby : hobbies) {\n        std::cout << hobby << " ";\n    }\n    std::cout << std::endl;\n\n    return 0;\n}
\n\n

Execution result:

\n
Name: Alexey\nAge: 30\nStudent? no\nHobbies: reading programming cycling
\n\n

Example 2: Handling nested objects

\n

JSON often contains nested structures. Let's look at an example with an address:

\n
{\n  "user": {\n    "name": "Maria",\n    "address": {\n      "city": "Moscow",\n      "street": "Tverskaya"\n    }\n  }\n}
\n\n

Accessing nested data:

\n


                
                

Blogs

Book Recommendations