Let's Parse stringified JSON in C++

Let's Parse stringified JSON in C++

ยท

3 min read

Let's agree that JSON is meant to be extracted using Javascript. Yes! Because its full form is Javascript object notation.๐Ÿ˜…๐Ÿ˜‚ But we should also agree that it's a lightweight data-interchange format that can exchange data between different programming languages and platforms. Actually, it is a language-independent data format. But if we want the speed of C++ and the magic of JSON we need to learn how we can extract data from JSON in C++. I would be very honest with you, it's sometimes really painful.

In this article, we will learn to parse JSON using JsonCPP. You can also use other libraries like RapidJSON or nlohmann. It becomes quite convenient once you use it. But TBH parsing in Javascript is super easy as we just need a single dot (.) / [ ] / simple methods to access its members or even optional chaining to avoid any further duck ups!

  1. Install jsoncpp library

     git clone https://github.com/open-source-parsers/jsoncpp
     cd jsoncpp
     python amalgamate.py
    

    The above commands will generate the include and dist folder, now you can copy them to your project folder and include them in your project accordingly. Alternatively, you can also use CMakeList or directly include in your build command to include the include and library paths. This is basically to use #include <jsoncpp/json/json.h> and #include <json/json.h> in our C++ code.

  2. Finally, we can now Parse our JSON

     #include <iostream>
     #include <jsoncpp/json/json.h>
     #include <json/json.h>
     #include <string>
     using namespace std;
    
     int main()
     {
       std::string inputJSON = "{\"nameOne\":\"Jayant Sharma\",\"nameTwo\":Jyoti Verma,\"city\":\"New York\"}";
    
       Json::Value outputDataAsJson;
       Json::CharReaderBuilder readerBuilder;
       std::string err;
       const std::unique_ptr<Json::CharReader> reader(readerBuilder.newCharReader());
    
       std::string name1, name2, city;
       if (reader->parse(inputJSON.c_str(), inputJSON.c_str() + inputJSON.length(), &outputDataAsJson, &err))
       {
         name1 = outputDataAsJson["nameOne"].asString();
         name2 = outputDataAsJson["nameTwo"].asString();
         city = outputDataAsJson["city"].asString();
       }
       else
       {
         cout << "Some error occurred" << endl;
       }
    
       cout << "value of key:Name1 in JSON" << name1 << endl;
       cout << "value of key:Name2 in JSON" << name2 << endl;
       cout << "value of key:city in JSON" << city << endl;
     }
    

In the above example, inputJson is a stringified JSON. On calling the parsing method on the reader with inputJson as a parameter it stores parsed JSON inside outputDataAsJson. Now we can easily fetch our values from this format.
Here Json::Value, Json::CharReaderBuilder & Json::CharReader are from the JsonCpp library.
This example uses the Json::Value and Json::Reader classes from the JsonCpp library. The Json::Value class is used to represent JSON values and the Json::Reader class is used to parse a JSON string and create a Json::Value object.

It is necessary to use these methods asString(), asInt(), asDouble(), etc. to get the value of the JSON element outputDataAsJson["nameOne"] and also use the [] operator to access the child element of a JSON object.

On similar lines, we can extract data from our stringified JSON. There are also other ways to do this but this was a simplistic way. If you have a better solution you can comment below your solution so as to help other readers.

Did you find this article valuable?

Support Vaibhav Garg by becoming a sponsor. Any amount is appreciated!

ย