2

I am using c++ code to read json string to retrieve value based on specific key names. Example of my json response from web API is in array format like below.

 [
    {
    "username": "123456",
    "useraddress": "abc",
    "data": [
                {
                    "schedule": true,
                    "task": "abc",
                    "risk": "1",
                } 
            ],
     "date": "0000-00-00"
    }
]

Like the above format is the actual response. I have to retrieve date value using key "date".

My code snippet:

{

std::stringstream jsonString;

boost::property_tree::ptree pt;

jsonString << ws2s(Info).c_str();

boost::property_tree::read_json(jsonString, pt);

std::string date = pt.get<std::string>("date");

}

'Info' in above snippet is wsstring containing json response data.

I can able to retrieve "date" if [] square brackets are removed manually. Since it is array format, if I pass without removing brackets, read_json throws error.

Can somebody help this out?

  • If you are parsing JSON-data at a scale this might be a more robust alternative: https://github.com/simdjson/simdjson – kometen Feb 12 '21 at 09:42
  • 1
    Property tree isn't a general purpose JSON parser, if you want to parse arbitrary JSON documents use a dedicated JSON parser like https://github.com/nlohmann/json – Alan Birtles Feb 12 '21 at 09:44
  • E.g. see https://stackoverflow.com/questions/2114466/creating-json-arrays-in-boost-using-property-trees – Alan Birtles Feb 12 '21 at 09:46
  • I think we should stop closing these questions. Instead we should be advertising Boost JSON. The question wasn't tagged property-tree, but is was tagged JSON (and titled as such_ – sehe Feb 12 '21 at 13:05
  • @sehe why boost JSON rather than any of the other myriad JSON libraries, seems opinion based and therefore off topic to me – Alan Birtles Feb 12 '21 at 17:59
  • 1
    @AlanBirtles It ws tagged [tag:c++] [tag:boost] [tag:json]. Color me silly – sehe Feb 12 '21 at 18:55

1 Answers1

0

Yeah. Boost Property Tree is a property tree library, not JSON.

You're in luck though, Boost has a JSON library now https://www.boost.org/doc/libs/1_75_0/libs/json/doc/html/index.html

Note: your input isn't valid JSON either, because JSON doesn't strictly allow trailing commas. You can enable them with an option in Boost JSON though:

Live On Compiler Explorer

#include <boost/json.hpp>
#include <iostream>

int main() {
    std::string input = R"(
         [
            {
            "username": "123456",
            "useraddress": "abc",
            "data": [
                        {
                            "schedule": true,
                            "task": "abc",
                            "risk": "1",
                        } 
                    ],
             "date": "0000-00-00"
            }
        ])";

    boost::json::parse_options options;
    options.allow_trailing_commas = true;
    auto json = boost::json::parse(input, {}, options);

    for (auto& el : json.as_array()) {
        std::cout << el.at("date") << "\n";
    }
}

Prints

"0000-00-00"
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Note that Boost JSON can also be used without boost: https://github.com/boostorg/json – sehe Feb 12 '21 at 10:15
  • Note also, Boost JSON has "databinding" customization points `value_to` and `value_from` which make working with your own datatstructures a lot less painful: https://godbolt.org/z/Yfn4ET – sehe Feb 12 '21 at 11:07