Moments of Inertia by Rachel Crawford

About     Archive     Categories     Tags     Feed     Projects    

Handling JSON with json.hpp

json.hpp is a single-header C++ library for handling JSON. It provides easy, clean ways to read from and write to JSON files. It’s written in C++ 11, which has pros (great functionality, trivial to integrate) and cons (won’t work with older compilers or those that don’t fully support C++ 11), but, like, it’s 2016, man. It’s about time to make use of modern C++, rather than just stare wistfully at it from afar.

Reading in JSON from a file is easy. Let’s say you’ve got a file called ‘data.json’ which looks like this:

  "info": {
    "name": "Rachel",
    "age": 22,
    "likesBacon": true

To read it into a C++ application, you use a std::ifstream to feed the data to an instance of nlohmann::json:

using json = nlohmann::json;
json j;
std::ifstream ifs("data.json");
if (!ifs.is_open()) {
  return false;
ifs >> j;

Next, you operate on j using a variety of syntax options to extract the data you want. json::find is a safe way to search the JSON for a particular attribute (field). Standard square-brackets syntax can be used to extract the value of an attribute, which can itself be another group of attributes. To make accessing the sub-attributes of an attribute easier you can just create another json object, like so:

Person person;

// verify that "info" attribute exists in j
if (j.find("info") != j.end()) {
  json info = j["info"];

  // verify that "name" attribute exists in info
  if (info.find("name") != info.end()) { = info["name"]; // == "Rachel"


And that’s how simple it is to extract data from a .json file and use it in an application.

Outputting to a file is easy, too. You just fill up a json object and then stream it out to an std::ofstream or something of the sort.

I really like this little library, although I’ve only scratched the surface of what you can do with it here. I wanted to make others aware of it for the next time they need to handle JSON in C++. There’s no standard library support for JSON in C++ like there is in other languages like Python and there’s not really a go-to non-standard option out there, so recommendations are useful. I’m using it to create an import system for Aseprite, which can export .json files to accompany sprite-sheets. More on that later.

comments powered by Disqus