Json C++ - For creating and parsing JSON with C++
JSON C++ is a utility for Modern C++
JSON C++ is a utility for Modern C++ to create and read JSON files.
Installation
Make sure you already have Git, GCC or Clang, CMake, GNU Make installed on your system.
Then clone, build and install with the following commands:
git clone https://github.com/nlohmann/json
cd json
mkdir build && cd build
cmake..
make
sudo make install
It will be Copied/Installed in directories similar to the ones below:
-- Install configuration: ""
-- Up-to-date: /usr/local/include
-- Installing: /usr/local/include/nlohmann
-- Installing: /usr/local/include/nlohmann/json.hpp
-- Installing: /usr/local/lib64/cmake/nlohmann_json/nlohmann_jsonConfig.cmake
-- Installing: /usr/local/lib64/cmake/nlohmann_json/nlohmann_jsonConfigVersion.cmake
-- Installing: /usr/local/lib64/cmake/nlohmann_json/nlohmann_jsonTargets.cmake
-- Installing: /usr/local/lib64/pkgconfig/nlohmann_json.pc
Basic usage example
To create a basic JSON.
vim hello-json.cpp
#include <iostream>
#include <nlohmann/json.hpp>
int main(){
nlohmann::json j;
j.push_back("Hello");
j.emplace_back("World");
std::cout << j << '\n';
}
g++ hello-json.cpp
and run:./a
Possible output:
["Hello","World"]
Printing only the first element of JSON:
#include <iostream>
#include <nlohmann/json.hpp>
int main(){
nlohmann::json j;
j.push_back("Hello");
j.emplace_back("World");
std::cout << j[0] << '\n';
}
Possible output:
"Hello"
Parsing
Example of a example.json
JSON file:
{
"number": 42,
"website": "Terminal Root",
"author": "Marcos Oliveira",
"parsing": true,
"details": {
"tutorial": "json C++",
"id": 1
},
"other": null
}
Print all content:
#include <iostream>
#include <nlohmann/json.hpp>
#include <fstream>
int main(){
std::ifstream file("./example.json");
nlohmann::json j;
file >> j;
std::cout << j << '\n';
}
Possible output, note that the display has been changed to alphabetical order:
{"author":"Marcos Oliveira","details":{"id":1,"tutorial":"json C++"},"number":42,"other":null,"parsing":true," website":"Terminal Root"}
Print content with formatting:
#include <iostream>
#include <nlohmann/json.hpp>
#include <fstream>
int main(){
std::ifstream file("./example.json");
nlohmann::json j;
file >> j;
std::cout << std::setw(2) << j << '\n';
}
Possible output,note that the display has been changed to alphabetical order :
{
"author": "Marcos Oliveira",
"details": {
"id": 1,
"tutorial": "json C++"
},
"number": 42,
"other": null,
"parsing": true,
"website": "Terminal Root"
}
Print only website content
#include <iostream>
#include <nlohmann/json.hpp>
#include <fstream>
int main(){
std::ifstream file("./example.json");
nlohmann::json j;
file >> j;
std::string website = j["website"];
std::cout << website << '\n';
}
Possible output:
Terminal Root
.
Print the contents of {"details": {"tutorial"}}
:
#include <iostream>
#include <nlohmann/json.hpp>
#include <fstream>
int main(){
std::ifstream file("./example.json");
nlohmann::json j;
file >> j;
std::string jstr = j["details"]["tutorial"];
std::cout << jstr << '\n';
}
Possible output:
json C++
.
For complete documentation visit repository and official page .
Comments