Create and manipulate a JSON variable - GitMasterNikanjam/nlohmann_json_examples GitHub Wiki
To create and manipulate a JSON variable in C++ using nlohmann/json, you can directly define and assign JSON objects, arrays, strings, numbers, booleans, and more. This library allows you to work with JSON as if you're handling native C++ types.
Here's a more detailed breakdown of how to set and work with JSON variables.
You can define JSON objects (similar to dictionaries or maps) by using key-value pairs.
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
// Creating a basic JSON object
json j;
j["name"] = "Alice"; // String value
j["age"] = 25; // Integer value
j["is_student"] = true; // Boolean value
std::cout << j.dump(4) << std::endl; // Dump the JSON object with 4 spaces indentation
return 0;
}
You can also create arrays (similar to vectors or lists) in JSON:
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
// Creating a JSON array
json j_array = {"C++", "Python", "JavaScript"};
std::cout << j_array.dump(4) << std::endl;
// Accessing elements
std::cout << "First language: " << j_array[0] << std::endl;
return 0;
}
You can create more complex structures, like nested objects and arrays:
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
// Creating a nested JSON object
json j;
j["person"]["name"] = "Bob"; // Nested JSON object
j["person"]["age"] = 30;
j["person"]["skills"] = {"C++", "Python"}; // Nested array
std::cout << j.dump(4) << std::endl; // Dump the JSON object with 4 spaces indentation
return 0;
}
You can append items to JSON arrays using push_back()
or emplace()
.
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
// Create an empty array
json j_array = json::array();
// Adding elements to the array
j_array.push_back("C++");
j_array.push_back("Python");
j_array.emplace_back("JavaScript");
std::cout << j_array.dump(4) << std::endl;
return 0;
}
You can also set a JSON variable by parsing a string that contains JSON data:
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
std::string json_string = R"({
"name": "Alice",
"age": 25,
"is_student": true,
"skills": ["C++", "Python", "JavaScript"]
})";
// Parsing JSON from a string
json j = json::parse(json_string);
std::cout << j.dump(4) << std::endl;
return 0;
}
You can update values in a JSON object just like updating values in a map:
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
json j;
j["name"] = "Alice";
j["age"] = 25;
// Update the value of a key
j["age"] = 26;
std::cout << j.dump(4) << std::endl;
return 0;
}
nlohmann/json
supports automatic type conversion between JSON types and C++ types.
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
// JSON object with various types
json j = {
{"name", "Alice"},
{"age", 25},
{"is_student", true}
};
// Access and convert to C++ types
std::string name = j["name"];
int age = j["age"];
bool is_student = j["is_student"];
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "Is student: " << std::boolalpha << is_student << std::endl;
return 0;
}
If you want to write your JSON data to a file:
#include <iostream>
#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
json j;
j["name"] = "Alice";
j["age"] = 25;
// Writing JSON to a file
std::ofstream file("output.json");
file << j.dump(4); // Pretty print with 4 spaces indentation
file.close();
std::cout << "JSON written to file!" << std::endl;
return 0;
}
Similarly, to read JSON from a file:
#include <iostream>
#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
// Reading JSON from a file
std::ifstream file("input.json");
json j;
file >> j;
std::cout << j.dump(4) << std::endl;
return 0;
}
- JSON objects are set using key-value pairs like a C++ map.
- JSON arrays are like C++ vectors.
- You can nest objects and arrays to represent more complex data structures.
- JSON values can be accessed and modified using the
[]
operator. - You can parse JSON from strings or serialize JSON to strings/files using
parse()
anddump()
.
This library is powerful yet easy to use, offering flexibility in creating, manipulating, and storing JSON data in C++.