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:
-
Download the header file from the latest release.
-
Include the header file in your project. Copy it to your
include
directory or any relevant directory in your project.
#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;
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;
}
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.
-
Go to the GitHub Release Page:
-
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).
- Look for a file named
-
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
orlibs
folder, or in the same directory as your source files.
- After downloading, extract the
/my_project
/include
json.hpp # The nlohmann/json header
/src
main.cpp # Your C++ source file
CMakeLists.txt # If using CMake
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;
}
-
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.