Parsing JSON Responses Of Unknown Format For Single Value - c++

I am creating a stock screener that sources data from ultiple apis. I would like to parse api responses from various apis for a single value.
I am using stock data apis that return JSON in different nested formats. Since the format of the JSON can be different, e.g. root could be an object or an array.. I am having trouble understanding how to go about this. I have successfully parsed JSON responses when the format is known. I am using qt, with no 3rd party libs, which seems to require that you parse these responses explcitly as I have done previously. How do I create a generic JSON parser that? Is this even possible?
Example: For this JSON response.. I would like to parse for "value"
{
"historical_data": [
{
"date": "2019-06-28",
"value": 197.92
}
],
"security": {
"id": "sec_agjrgj",
"company_id": "com_NX6GzO",
"stock_exchange_id": "sxg_ozMr9y",
"name": "Apple Inc",
"code": "EQS",
"currency": "USD",
"ticker": "AAPL",
"composite_ticker": "AAPL:US",
"figi": "BBG000B9Y5X2",
"composite_figi": "BBG000B9XRY4",
"share_class_figi": "BBG001S5N8V8"
},
"next_page": null
}
I also want to parse this JSON repsone for "value"
{
"date": "2019-06-28",
"value": 197.92
}
I am trying to not write a parser function for each api I use. I would like to be able to find if the JSON has a "value" and if so.. return its value.

You can use JsonPath for this. For example, using the jsoncons implementation of JsonPath, you can write
#include <iostream>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpath/json_query.hpp>
using namespace jsoncons;
int main()
{
std::string data = /* your JSON response */;
json root = json::parse(data);
// json_query returns an array of items that match the JsonPath expression
json result = jsonpath::json_query(root, "$..value");
for (const auto& item : result.array_range())
{
std::cout << item << "\n";
}
}
Output:
197.92

Related

how to obtain data argument to provide to mustache template in moodle

In moodle (4.0) I have the need to call
$this->output->render_from_template('core_courseformat/local/content/section/cmlist', $sectionData);
From within a renderer. The goal is to render the normal, native, cmlist component in a particular place on the page. But the way that I am currently getting the value of $section does not seem to work. My template renders nothing. I can see from the github source that this template expects data in this format:
Example context (json):
{
"cms": [
{
"cmitem": {
"cmformat": {
"cmname": "<a class=\"aalink\" href=\"#\"><span class=\"instancename\">Forum example</span></a>",
"hasname": "true"
},
"id": 3,
"module": "forum",
"extraclasses": "newmessages"
}
},
{
"cmitem": {
"cmformat": {
"cmname": "<a class=\"aalink\" href=\"#\"><span class=\"instancename\">Assign example</span></a>",
"hasname": "true"
},
"id": 4,
"module": "assign",
"extraclasses": ""
}
}
],
"hascms": true,
"showmovehere": true,
"movingstr": "Moving this activity: folder example",
"cancelcopyurl": "#",
"movetosectionurl": "#",
"strmovefull": "Move 'folder example' to this location"
}
}}
https://github.com/moodle/moodle/blob/1d99ba19a21d57e9f1ed4211a8eeee00e50b7baf/course/format/templates/local/content/section/cmlist.mustache
But here's the challenge. How do I get an object in that format with the data needed to feed the template so it can render the correct CM list items?
Currently I am tring:
$sectionData = get_fast_modinfo($course->id)->get_section_info($section);
But it doesn't seem to return the data structured in the right way.
Any help appreciated.
You can use the function export_for_template of cmlist render class.
Something like this:
$cmlist = new \core_courseformat\output\local\content\section($format, $section);
$data->cmlist = $cmlist->export_for_template($OUTPUT);
and then send the data to the template.
I recommend you imitate the behavior of a moodle as he performs here:
https://github.com/moodle/moodle/blob/7ce003b666a66b465ce9335f430a6e4d3535a7f1/course/format/classes/output/local/content/section.php#L223

How can I parse unpredictable JSON fields in c++?

So, I access an API an receive data in JSON format as shown below. I'm looking to draw a chart with the dates on the "x" axis and the Data on the "y" axis. I don´t know what dates I will be getting, and they will be plenty. I want to save that data in an array/vector of objects that contain a structure containing the date and value at that date. Because this isn't a key-value relation, I do not know how to parse it, do you?
Structure
typedef struct
{
float Data;
string date;
}Data_t;
JSON received
"Information: Data": {
"2021-06-01 16:00:01": {
"Data": "139.5578"
},
"2021-05-28": {
"Data": "137.7645"
},
"2021-05-21": {
"Data": "135.8931"
},
"2021-05-14": {
"Data": "133.6110"
}
...
...
...

How to read Json string starting with square brackets in it? [duplicate]

This question already has an answer here:
How to use boost::property_tree to parse JSON with array root
(1 answer)
Closed 2 years ago.
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?
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"

Get keys from Json with regex Jmeter

I'm hustling with regex, and trying to get the id's from this body.
But only the id´s in the members list, and not the id in the verified key. :)
To clarify, I'm using Regular Expression Extractor in JMeter
{
"id": "9c40ffca-0f1a-4f93-b068-1f6332707d02", //<--not this
"me": {
"id": "38a2b866-c8a9-424f-a5d4-93b379f080ce", //<--not this
"isMe": true,
"user": {
"verified": {
"id": "257e30f4-d001-47b3-9e7f-5772e591970b" //<--not this
}
}
},
"members": [
{
"id": "88a2b866-c8a9-424f-a5d4-93b379f780ce", //<--this
"isMe": true,
"user": {
"verified": {
"id": "223e30f4-d001-47b3-9e7f-5772e781970b" //<--not this
}
}
},
{
"id": "53cdc218-4784-4e55-a784-72e6a3ffa9bc", //<--this
"isMe": false,
"user": {
"unverified": {
"verification": "XYZ"
}
}
}
]
}
at the moment I have this regex :
("id": )("[\w-]+")
But as you can see here it returns every guid
Any ideas on how to go on?
Thanks in advance.
Since the input data type is JSON, it is recommended to use the JMeter's JSON Path Extractor Plugin.
Once you add it, use the
$.members[*].id
JSON path expression to match all id values of each members in the document that are the top nodes.
If you may have nested memebers, you may get them all using
$..members[*].id
You may test these expressions at https://jsonpath.com/, see a test:

Setting output contexts in Dialogflow

Using the C# client library for Dialogflow, I am trying to set the output context in a webhook response. However, the output context field is read only. This is my code:
WebhookResponse response = new WebhookResponse
{
FulfillmentText = "This is a test",
OutputContexts = ... //Regardless of what I try and set OutputContexts to be, I get the error "property or indexer 'WebhookResponse.OutputContexts' cannot be assigned to -- it is read only"
};
How do I set the output context?
I know this is an old question but just in case someone has the same problem.
You can not assign a new list to OutputContexts, you have to add them to the list:
For example:
response.OutputContexts.Add(new Context
{
Name = $"{request.Session}/your_context",
LifespanCount = 1
});
I think the response json which you are forming is wrong.
Below is the correct json response which you need to send:
{
"fulfillmentText = "This is a test",
"outputContexts": [
{
"name": "projects/project_id/agent/sessions/session_id/contexts/your_context",
"lifespanCount": 5,
"parameters": {
"foo": "bar",
"foo1": "bar1"
}
}
],
"followupEventInput": {
"name": "even_name"
}
}