Read a sub json using boost property tree - c++

I need to read a field in a JSON file which itself is a JSON. I need to read the JSON field in one go. Is there any way available? Sample JSON I am trying to read is provided below.
enter code here
{
"responses": [
{
"id": "1",
"status": 200,
"headers": {
"OData-Version": "4.0",
"Content-Type":"application/json;odata.metadata=minimal;odata.streaming=true"
},
"body": {
"createdDateTime": "2021-04-22T09:24:59.394Z",
"displayName": "Test1",
"visibility": "public",
"isMembershipLimitedToOwners": false,
"discoverySettings": { "showInTeamsSearchAndSuggestions": true },
"memberSettings": {
"allowCreateUpdateChannels": true,
"allowCreateUpdateRemoveConnectors": true
},
"guestSettings": {
"allowCreateUpdateChannels": true,
"allowDeleteChannels": false
},
"messagingSettings": {
"allowUserEditMessages": true,
"allowChannelMentions": true
},
"funSettings": {
"allowGiphy": true,
"allowCustomMemes": true
}
} } ]
}
I am trying to read the "body" field using the code below (json is read in boost::property_tree::ptree jsonBatchResponse jsonBatchResponse). But strBody is empty and it doesn't read the "Body" field correctly. :
enter code here
for (auto& v : jsonBatchResponse.get_child("responses"))
{
std::string strID = v.second.get<std::string>("id", "");
std::string strStatus = v.second.get<std::string>("status", "");
std::string strBody = v.second.get<std::string>("body", "");
}
It looks like v.second.getstd::string("body", "") is not the right way to read the JSON field. Is there any other way available (other than reading individual fields in the JSON value)? Please let me know.

The body is not a string.
So, getting the child object would be in order:
for (auto const& v : jsonBatchResponse.get_child("responses")) {
std::string strID = v.second.get<std::string>("id", "");
std::string strStatus = v.second.get<std::string>("status", "");
ptree const& body = v.second.get_child("body");
}
If you add some output to that loop with e.g.
std::cout << std::quoted(strID) << "\n";
std::cout << std::quoted(strStatus) << "\n";
write_json(std::cout, body);
It will print Live On Coliru
"1"
"200"
{
"createdDateTime": "2021-04-22T09:24:59.394Z",
"displayName": "Test1",
"visibility": "public",
"isMembershipLimitedToOwners": "false",
"discoverySettings": {
"showInTeamsSearchAndSuggestions": "true"
},
"memberSettings": {
"allowCreateUpdateChannels": "true",
"allowCreateUpdateRemoveConnectors": "true"
},
"guestSettings": {
"allowCreateUpdateChannels": "true",
"allowDeleteChannels": "false"
},
"messagingSettings": {
"allowUserEditMessages": "true",
"allowChannelMentions": "true"
},
"funSettings": {
"allowGiphy": "true",
"allowCustomMemes": "true"
}
}
BONUS: Using a proper JSON library instead
Boost Property Tree is NOT a JSON library, and therefore has a lot of limitations.
Instead I suggest using Boost JSON:
Live On Coliru
#include <boost/json.hpp>
#include <boost/json/src.hpp>
#include <iostream>
namespace json = boost::json;
extern std::string sample;
int main() {
json::object jsonBatchResponse = json::parse(sample).as_object();
for (auto& v : jsonBatchResponse["responses"].as_array()) {
auto& res = v.as_object();
json::value id = res["id"], // string
status = res["status"], // integer
body = res["body"]; // object
std::cout << id << "\n";
std::cout << status << "\n";
std::cout << body << "\n";
}
}
std::string sample = R"(
{
"responses": [{
"id": "1",
"status": 200,
"headers": {
"OData-Version": "4.0",
"Content-Type": "application/json;odata.metadata=minimal;odata.streaming=true"
},
"body": {
"createdDateTime": "2021-04-22T09:24:59.394Z",
"displayName": "Test1",
"visibility": "public",
"isMembershipLimitedToOwners": false,
"discoverySettings": {
"showInTeamsSearchAndSuggestions": true
},
"memberSettings": {
"allowCreateUpdateChannels": true,
"allowCreateUpdateRemoveConnectors": true
},
"guestSettings": {
"allowCreateUpdateChannels": true,
"allowDeleteChannels": false
},
"messagingSettings": {
"allowUserEditMessages": true,
"allowChannelMentions": true
},
"funSettings": {
"allowGiphy": true,
"allowCustomMemes": true
}
}
}]
}
)";
Prints
"1"
200
{"createdDateTime":"2021-04-22T09:24:59.394Z","displayName":"Test1","visibility":"public","isMembershipLimitedToOwners":false,"discoverySettings":{"showInTeamsSearchAndSuggestions":true},"memberSettings":{"allowCreateUpdateChannels":true,"allowCreateUpdateRemoveConnectors":true},"guestSettings":{"allowCreateUpdateChannels":true,"allowDeleteChannels":false},"messagingSettings":{"allowUserEditMessages":true,"allowChannelMentions":true},"funSettings":{"allowGiphy":true,"allowCustomMemes":true}}

Related

Faulty JSON is getting generated using boost::property_tree::ptree

I am using boost::property_tree::ptree to add data and create JSON file. The following is the recursive code that I have written -
using Strings = vector<string>;
Strings _headers;
map<string, Strings> _subHeaders;
namespace pt = boost::property_tree;
void flushHeader(pt::ptree& headersNode, const Strings& headers) const {
for(auto& header : headers) {
pt::ptree headerNode;
pt::ptree subHeaderNode;
pt::ptree subHeaderObjNode;
headerNode.put(header, "");
if(_subHeaders.find(header) != _subHeaders.end()) {
flushHeader(subHeaderObjNode, _subHeaders.find(header)->second);
subHeaderNode.push_back(make_pair("", subHeaderObjNode));
headerNode.put_child(pt::ptree::path_type(header, '|'), subHeaderNode);
}
headersNode.push_back(make_pair("", headerNode));
}
}
void flushData(pt::ptree& parent) const {
pt::ptree headersNode;
flushHeader(headersNode, _headers);
parent.put_child("Headers", headersNode);
}
JSON file created using the above code is something like this -
"Headers": [
{
"A": ""
},
{
"B": [
[
{
"X": ""
},
{
"Y": ""
}
]
]
}
]
There are two brackets - [ after the value B whereas ideally only one [ should be present. So I want my JSON to look like this -
"Headers": [
{
"A": ""
},
{
"B": [
{
"X": ""
},
{
"Y": ""
}
]
}
]
I hope I have explained the problem clearly. What changes can I make in my code to get the desired JSON file?
Thanks.

Mongo query to find not null string inside list of objects

I want to query an array of objects with a particular key should have text in it.
this is the query I have tried
to find disclaimer.text exists and not empty. But It always prints 0
db.slideshows.count({"config.slides": { $elemMatch: {disclaimer: {"text" : {"$exists" : true, "$ne" : ""} }} } })
This is my data
{
"id": 1002,
"config": {
"firstSlide": "vehicle",
"slides": [
{
"slideKey": "sk1",
"disclaimer": {
"text": ""
}
},
{
"slideKey": "sk2"
}
]
}
}
{
"id": 1003,
"config": {
"firstSlide": "book",
"slides": [
{
"slideKey": "sk3",
"disclaimer": {
"text": "Hello"
}
},
{
"slideKey": "sk4"
}
]
}
}
{
"id": 1004,
"config": {
"firstSlide": "book",
"slides": [
{
"slideKey": "sk3",
"disclaimer": {
"text": "nope"
}
},
{
"slideKey": "sk4",
"disclaimer": {
"text": ""
}
}
]
}
}
I want all the rows which have not empty disclaimer.text. ex. in the above set I need to get id 1004 and 1003 as a result.
Try
db.slideshows.count({
"config.slides": {
$elemMatch: {
"disclaimer.text": {
"$exists": true,
"$ne": ""
}
}
}
})

Keyname not found in json

I'm trying to create my own Telegram bot as a project and don't want to use any of the libraries already out there that already do all the hard work for me as I want to do this for myself as on going self learning.
I'm using CPPRESTSDK and trying to get values from the JSON back from telegram.
Here is an example of the JSON
{
"ok": true,
"result": [
{
"update_id": 534699960,
"message": {
"message_id": 159183,
"from": {
"id": HIDDEN,
"is_bot": false,
"first_name": "Hawke",
"username": "XXXXXXXX"
},
"chat": {
"id": HIDDEN,
"title": "CHANNEL_NAME_HIDDEN",
"username": "HIDDEN",
"type": "supergroup"
},
"date": 1548427328,
"text": "Nope, at work"
}
}
]
}
I'm trying to read the text value but I'm getting Keyname not found when trying to access result. The above JSON is stored into a file once retrieving the JSON from telegram.
try {
string_t importFile = U("json.txt");
ifstream_t f(importFile);
stringstream_t s;
json::value v;
if (f) {
s << f.rdbuf();
f.close();
v = json::value::parse(s);
auto results_array = v.at(U("result")).as_array();
}
}
catch (web::json::json_exception& excep) {
std::cout << excep.what();
}

JSON Data using Boost

Is it possible to read the following data below using boost?
{
"ok": true,
"result": [
{
"update_id": 1235285,
"message": {
"message_id": 2,
"from": {
"id": 3325446,
"is_bot": false,
"first_name": "Test",
"language_code": "en-PH"
},
"chat": {
"id": 12532541,
"first_name": "Test Account",
"type": "private"
},
"date": 152014521,
"text": "Test Message"
}
}
]
}
You could see the linked post in comment,
To summarize you can have like following to read from a file say mfile.json :
boost::property_tree::ptree pt;
boost::property_tree::read_json("myfile.json", pt);
print_contents( pt );
where print_contents is:
void print_contents( const boost::property_tree::ptree& pt)
{
using boost::property_tree::ptree;
for (const auto& x: pt )
{
std::cout << x.first << ": " << x.second.get_value<std::string>() << std::endl;
print_contents(x.second);
}
}
See Here
I could have closed it as duplicate, but looks like there was no "better" post for reading a json file

c++ json-nlohmann accessing the list elements

I'm trying to use json-nlohmann library to read JSON files in C++
So far I got along with it preatty well, but now I'm trying to access elements of the list in given json. JSON:
{
"GameMap": "path_to_game_map",
"Objects": [
{ "object_1": { "Transform": { "Position": { "X": 1, "Y": 2 }, "Rotation": { "X": 3.5, "Y": 8.2 }, "Scale": { "X": 1, "Y":1 } },
"Components": [
{ "0": { "data": { "some_data": "false" } } },
{ "1": { "data": { "some_data": "false" } } },
{ "2": { "data": { "some_data": "false" } } }
] } },
{ "object_2": { "Transform": { "Position": { "X": 1, "Y": 2 }, "Rotation": { "X": 3.5, "Y": 8.2}, "Scale": { "X": 1, "Y":1 } },
"Components": [
{ "0": { "data": { "some_data": "false" } } },
{ "1": { "data": { "some_data": "false" } } },
{ "2": { "data": { "some_data": "false" } } }
] } }
]
}
Where I'm trying to access each Component and read it's key value.
I've got an object out for every Component. But I just cannot figure out how to read its key and value.
Help! Please.
When you iterate a JSON object, say j, you can access the key and value like this:
for (json::iterator it = j.begin(); it != j.end(); ++it) {
std::cout << it.key() << " : " << it.value() << "\n";
}
you can also now use for (auto& element : j.items()) and then element.key() and element.value() inside the loop.