Introduction: Why is JSON needed in C++?
\nJSON (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++.
\nIn 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.
\nTo 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\nInstalling the nlohmann/json Library
\nBefore 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\nMethod 1: Download the json.hpp file
\n- \n
- Go to the nlohmann/json GitHub repository. \n
- Download the
json.hppfile from the latest release. \n - Place it in your project folder. \n
- Include it in your code:
#include "json.hpp"\n
Method 2: Use a package manager (vcpkg / Conan)
\nIf you use CMake, the easiest way is to install the library via vcpkg:
\nvcpkg install nlohmann-json\nOr via Conan:
\nconan install nlohmann_json/3.11.2@\n\nAfter installation, include the library in your code:
\n#include <nlohmann/json.hpp>\nusing json = nlohmann::json;\n\nNow you are ready to go!
\n\nParsing JSON in C++: Reading Data
\nParsing 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\nExample 1: Parsing a JSON string
\nSuppose 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\nParsing 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\nExecution result:
\nName: Alexey\nAge: 30\nStudent? no\nHobbies: reading programming cycling\n\nExample 2: Handling nested objects
\nJSON 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\nAccessing nested data:
\nBlogs
Book Recommendations
Read also
Advanced-Level Testing in Java: Unit, Integration, E2E
How to Disable Dead Code Warnings at the Crate Level in Rust: A 2025 Guide
Kotlin on LeetCode: How to Solve Problems and Level Up Your Skills
Monorepo with PHP: Code Organization and Best Practices
Docker and TypeScript: Application Containerization for Developers