Categories: Software Development

Learning JsonCpp

If you want to learn how to parse and generate JSON with C++ you have come to the right place.

Read on to find out how to:

How to use Json Cpp for Parsing JSON files in C++ with iterators
JsonCpp Cheat Sheet

JsonCpp is a good solid C++ library to work with. I hope the code samples below show that you can easily work with C++ and JSON files. What’s interesting about JSONCPP is that even Google uses it in their C++ library for their Data Layer API.

Another cool thing about JsonCpp is that it gives you the ability to open JSON configuration files. This means you can re-use configs and build a faster more performant project out of a frontend web development project. You can use C++ and Node.js and get the best of both worlds through sharing JSON files and JsonCpp!

I tagged v1.0.0 of the code on github. It loads and saves a JSON file properly. I ran into a weird issue with iterators but I think that’s my own fault for not completely understanding how to reference/dereference them.

The issue I’m having is that JsonCpp doesn’t seem to be packaged up for distribution on Ubuntu or other distros.

JsonCpp is great to work with, but the API docs need more of a tutorial. I hope that my code in learning-jsoncpp can serve as kind of a tutorial.

Saving an Object to JSON

Here’s a example of the JSON-specific code to save an object to JSON:

void AddressBook::JsonSave(const char* filename) {
    ofstream out(filename, ofstream::out);
    Json::Value book_json(Json::objectValue), contacts_json(Json::arrayValue);
    for (vector<Contact>::iterator it = contacts_.begin(); it != contacts_.end(); ++it) {
        contacts_json.append((*it).ToJson());
    }
    book_json["contacts"] = contacts_json;
    out << book_json;
    out.close();
}

Json::Value Contact::ToJson() const {
    Json::Value value(Json::objectValue);
    value["name"] = name_;
    value["phone_number"] = phone_number_;
    return value;
}

Loading JSON List/Vector of Objects

Here’s an example of loading a list/vector of objects from JSON:

void AddressBook::JsonLoad(const char* filename) {
    ifstream in(filename);
    Json::Value book_json;
    in >> book_json;
    for (Json::Value::iterator it = book_json["contacts"].begin(); it != book_json["contacts"].end(); ++it) {
        AddPerson((*it)["name"].asString(), (*it)["phone_number"].asString());
    }
    in.close();
}

void AddressBook::AddPerson(string const &name, string const &phone_number) {
    Contact contact = Contact();
    contact.set_name(name);
    contact.set_phone_number(phone_number);
    contacts_.push_back(contact);
}

Explanation of Loading JSON Code Example

Okay let’s break down the code above!

First we’re defining a method, JsonLoad, in the AddressBook class. It accepts a filename string argument:

void AddressBook::JsonLoad(const char* filename) {

Then we are creating an input stream using ifstream:

ifstream in(filename);

And we create a variable that will store the parsed JSON value:

Json::Value book_json;

Then all we have to do is pipe the input stream into the JSON value object. The JsonCpp library defines the >> operator and will take care of parsing of the input stream into JSON:

in >> book_json;

Check out learning-jsoncpp!

You can check out the learning-jsoncpp project with source code on Github to see a fully functioning example of using JSONCPP.

I have also written a Perl6 tutorial on parsing JSON.

Rudolf Olah is a software development expert with over 8 years of professional software developer experience. He has produced the video courses "Reactive Programming in Python with RxPy, PyQt5 and Tornado" and "Learning AngularJS Testing" for PacktPublishing. Rudolf offers web development training courses for individual developers and for web development teams. He writes about tech leadership, career coaching and project management.