Install and beginning - GitMasterNikanjam/nlohmann_json_examples GitHub Wiki

Since nlohmann/json is header-only, you can simply download the header file from the GitHub repository:

  1. Download the header file from the latest release.

  2. Include the header file in your project. Copy it to your include directory or any relevant directory in your project.

2. Use nlohmann/json in Your C++ Programs

Step 1: Include the JSON Header in Your Source Code

#include <nlohmann/json.hpp>

You can now use nlohmann::json as a type in your code. It's typically aliased as json for easier use:

using json = nlohmann::json;

Step 2: Sample C++ Program with nlohmann/json

Here is a full example showing how to use the library in a C++ program:

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

using json = nlohmann::json;

int main() {
    // Create a JSON object
    json j;
    j["name"] = "Alice";
    j["age"] = 25;
    j["is_student"] = true;
    j["skills"] = {"C++", "Python", "JavaScript"};

    // Serialize JSON to string
    std::string jsonString = j.dump();
    std::cout << "Serialized JSON: " << jsonString << std::endl;

    // Parse JSON from string
    json parsed = json::parse(jsonString);
    std::cout << "Name: " << parsed["name"] << std::endl;
    std::cout << "Age: " << parsed["age"] << std::endl;

    return 0;
}

Step 3: Compile the Program

If you manually included the header file:

You can compile this program using any C++ compiler. For example, using g++:

g++ -std=c++11 -o my_program main.cpp

In the nlohmann/json GitHub releases, you'll find various files such as source code archives, binaries, and sometimes other assets. However, since nlohmann/json is a header-only library, you only need the header file to use the library in your C++ program. Below are the steps to find and download the right file.

Steps to Download the Correct File

  1. Go to the GitHub Release Page:

  2. Download the Correct Archive:

    • Look for a file named json.hpp, which is the main header file you need.
    • Alternatively, download the single-header file archive. Usually, it’s a file like json.hpp or inside an archive (e.g., .zip or .tar.gz) with the following format:
      • json-<version>.single-header.hpp (e.g., json-v3.11.2.single-header.hpp)
      • You can extract this and use it in your project.

    Here’s what you should look for:

    • json.hpp (the header-only file).
  3. Where to Put the File in Your Project:

    • After downloading, extract the .hpp file (if it's in a compressed archive).
    • Place this file in an appropriate directory within your project, usually in an include or libs folder, or in the same directory as your source files.

Example Folder Structure:

/my_project
  /include
    json.hpp    # The nlohmann/json header
  /src
    main.cpp    # Your C++ source file
  CMakeLists.txt # If using CMake

How to Include it in Your C++ Program:

In your C++ program, you simply include this header file:

#include "json.hpp"  // Assuming the file is in the same folder or in include path

using json = nlohmann::json;

int main() {
    json j = {{"name", "Alice"}, {"age", 25}, {"is_student", true}};
    std::cout << j.dump(4) << std::endl;
    return 0;
}

Conclusion:

  • Download the single-header file (typically named json.hpp or found within a .zip/.tar.gz archive).
  • Place the header file in your project and include it in your C++ code.

That’s all you need to use nlohmann/json! No need for complicated installations or multiple files—just this single header will get you started.

⚠️ **GitHub.com Fallback** ⚠️