Read data from JSON file in C++
In this tutorial, we will learn how to read a JSON file in C++.
Firstly, what is a JSON file?
JSON is short for JavaScript Object Notation. A JSON file stores data structures and objects in an organised and easy to access manner in JavaScript Object Notation (JSON) format. It is commonly used for transmitting data between web application and server.
Reading data from a JSON file
In this tutorial, I shall be using the boost library to read the JSON file. Within the boost library, we have a sublibrary called property tree. Property tree allows us to handle the tree of properties and is used to represent various files like XML, INI, JSON files etc.
In the following program, we first make a root of the property tree type and load the JSON file into it. Then, we use the declare variables roll, name, class1 to store the values of “roll no”, “name” and “class” respectively. Thereafter, we display all the values. For the values of the “address“, we use for loop. In the loop, the different values of the vector are displayed.
The JSON file with file name “file.json” , that we will be using is :
{ "name": "Aryan", "class": "Masters in Maths with Comp Sc.", "roll no": 3526, "address": { "city": "Delhi", "house no": "432-B", "locality": "Omaxe City" } }
Program: Read data from a JSON File in C++
#include <iostream> #include <string.h> #include <exception> #include <boost/property_tree/json_parser.hpp> #include <boost/property_tree/ptree.hpp> #include "json.hpp" using namespace std; using json = nlohmann::json; namespace pt = boost::property_tree; int main() { pt::ptree root; pt::read_json("file.json", root); // Load the json file in this ptree int roll = root.get<int>("roll no"); //read and save the roll no in *roll* string name = root.get<string>("name"); //read and save the name in *name* string class1 = root.get<string>("class"); //read and save the class in *class1* cout << "name : " << name << endl; //getting the output of all cout << "roll no : " << roll << endl; cout << "class : " << class1 << endl << "address : " << endl << endl; for (pt::ptree::value_type & v : root.get_child("address")) { cout << v.first << endl; cout << " "<<v.second.data() << endl; } return 0; }
Output:
name : Aryan roll no : 3526 class : Masters in Maths with Comp Sc. address : city Delhi house no 432-B locality Omaxe City
You can also make a JSON object within the program and read it similarly. In the below program, we first make an object of JSON type and read its contents. Then, using the for loop, we get the output. Below is a program depicting the same :
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> #include <iostream> using namespace std; namespace pt = boost::property_tree; int main() { stringstream s; s << "{ \"root\": { \"values\": [1, 2, 3, 4, 5 ] } }"; pt::ptree root; // Creates a root pt::read_json(s, root); // Loads the json file in this ptree for (pt::ptree::value_type& v : root.get_child("root.values")) { cout << v.second.data() << endl; //gives the output } return 0;
Output :
1 2 3 4 5
Hope this was helpful. Enjoy coding!
Also learn :
Nested Class Constructor in C++
Multiple ways to split a string in C++
Hi. Thanks for the article. But there is an issue…
Instead of #include “json.hpp” we should use :
#include
And you should have mentioned to download nlohmann-json also. I downloaded it from powershell using vcpkg.
It’s actually #include
I mean instead of “json.hpp” we should use nlohmann/json.hpp placed between “”symbols
How to take values of the “values” array into another array out of json, so that it can later on be used to perform further operations??
Please help!