JSON parsing using nlohmann json

Hemant Yadav picture Hemant Yadav · Feb 16, 2018 · Viewed 9.9k times · Source

I am trying to parse the JSON structure using nlohmann's json.hpp . But I am not to create the JSON structure from the string. I have tried all the way, but still it is failing.

My requirement is to:

1) Create the JSON structure from the string.

2) Find the value of "statusCode" from it.

After trying for so long time, I am really getting doubt, does nlohmann's json parser support nested JSON or not.

#include "json.hpp"
using namespace std;

int main(){

    // giving error 1
    nlohmann::json strjson = nlohmann::json::parse({"statusResp":{"statusCode":"S001","message":"Registration Success","snStatus":"Active","warrantyStart":"00000000","warrantyEnd":"00000000","companyBPID":"0002210887","siteBPID":"0002210888","contractStart":"00000000","contractEnd":"00000000"}});

    // Giving error 2:
   auto j= "{
    "statusResp": {
        "statusCode": "S001",
        "message": "Registration Success",
        "snStatus": "Active",
        "warrantyStart": "20170601",
        "warrantyEnd": "20270601",
        "companyBPID": "0002210887",
        "siteBPID": "0002210888",
        "contractStart": "00000000",
        "contractEnd": "00000000"
    }
   }"_json;

   // I actually want to get the value of "statusCode" code from the JSOn structure. But no idea how to parse the nested value.
    return 1;

}

Below are the error for both the initialisations:

//ERROR 1:
test.cpp: In function 'int main()':
test.cpp:17:65: error: expected '}' before ':' token
     nlohmann::json strjson = nlohmann::json::parse({"statusResp":{"statusCode":"S001","message":"Registration Success","snStatus":"Active","warrantyStart":"00000000","warrantyEnd":"00000000","companyBPID":"0002210887","siteBPID":"0002210888","contractStart":"00000000","contractEnd":"00000000"}});

// ERROR 2:
hemanty@sLinux:/u/hemanty/workspaces/avac/cb-product/mgmt/framework/src/lib/libcurl_cpp$g++ test.cpp -std=gnu++11
test.cpp: In function 'int main()':
test.cpp:27:17: error: expected '}' before ':' token
     "statusResp": {

Answer

super picture super · Feb 19, 2018

Since " is the character to begin and end a string literal you can not have a " character inside a string without putting a \ before it.

std::string str = " "statusCode":"5001" "; //This does not work

std::string str = " \"statusCode\":\"5001\" "; //This will work

An easier alternative when you want to make strings with a lot of " in them is to use the R"" string literal. Then you can write it like so.

std::string str = R"("statusCode":"5001")";

If we now transfear this to your json example, the correct way to parse the strings would be one of the following.

auto j3 = json::parse("{ \"happy\": true, \"pi\": 3.141 }");
// and below the equivalent with raw string literal
auto j3 = json::parse(R"({"happy": true, "pi": 3.141 })");

//Here we use the `_json` suffix 
auto j2 = "
{
    \"happy\": true,
    \"pi\": 3.141
}"_json;
// Here we combine the R"" with _json suffix to do the same thing.
auto j2 = R"(
{
    "happy": true,
    "pi": 3.141
  }
)"_json;

Examples taken from the readme