how can I serialize 2 sets using nlohmann/json.hpp - c++

I have two unordered sets of pairs (X,Y) implemented using boost hash and I would like to convert them to a Json file that has a special format.
unordered_set<pair<int,int>> visited, cleaned
. I want them to be represented this way in Json format using nlohmann/json.hpp C++:
{
"visited": [
{
"X": 2,
"Y": 2
},
{
"X": 3,
"Y": 0
},
{
"X": 3,
"Y": 1
},
{
"X": 3,
"Y": 2
}
],
"cleaned": [
{
"X": 2,
"Y": 2
},
{
"X": 3,
"Y": 0
},
{
"X": 3,
"Y": 2
}
],
}
can anyone help me with the c++ code for this part?
my code is
for (auto it = visited.begin(); it != visited.end(); ++it)
{
j2["visited"]["X"]=it->second;
j2["visited"]["Y"] = it->first;
}
for (auto it = cleaned.begin(); it != cleaned.end(); ++it)
{
j2["cleaned"]["X"] = it->second;
j2["cleaned"]["Y"] = it->first;
}
and it produces:
{
"cleaned": {
"X": 3,
"Y": 2
},
"visited": {
"X": 3,
"Y": 2
}
}

The JSON format you want contains arrays. Use something like this to explicitly create
them:
nlohmann::json arr;
for (auto it = visited.begin(); it != visited.end(); ++it) {
nlohmann::json o;
o["X"] = it->second;
o["Y"] = it->first;
arr.push_back(o);
}
j2["visited"] = arr;
And similarly for the second part.

Related

Postman test save variable from JSON response body

I have json response body like this
{
"header": {
"process_time": "27.842975",
"server_time": "",
"reason": "",
"error_code": 0,
"status_code": 200
},
"data": {
"idx": "2",
"component": {
"id": 123,
"name": "product",
"properties": {
"button_notification": false
},
"title": "",
"data": [
{
"status": 4,
"product_id": 1112323,
"stock": 21
},
{
"status": 4,
"product_id": 1114322,
"stock": 13
},
{
"status": 4,
"stock": 8,
"product_id": 1115342
},
{
"stock": 5,
"status": 4,
"product_id": 1115234
},
{
"stock": 5,
"status": 4,
"product_id": 1115442
}
]
}
},
"errors": []
}
I want to save two product_id values which have the most two stock than other
here my code
var jsonData = JSON.parse(responseBody);
let productId-1 = jsonData.data.component.data[2].product_id
let productId-2 = jsonData.data.component.data[3].product_id
pm.environment.set("productId-1",productId-1)
pm.environment.set("productId-2",productId-2)
The most simple solution to your problem would be implementing bubble sort for finding the product with the most stock.
let arr = jsonData.data.component.data;
for(let i = 0; i < arr.length - 1; i++){
for(let j = 0; j < arr.length - 1 - i; j++){
if (arr[j].stock < arr[j+1].stock) {
let temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
pm.environment.set("productId-1", arr[0]); //product with most stock
pm.environment.set("productId-2", arr[1]); //product with second most stock

ChartJS Tooltips with time axis and multiple datasets

So i have a bar chart set up to display two different datasets that take place at more or less the same time, but have some missing data from hour to hour:
The data in this graph is stored in the format {x:timestamp, y:value}, and bars are all located at the correct positions, however some of the tooltips are wrong:
In this example, my mouse is highlighted over the "1AM" bar for dataset "0", yet the timestamp says it is at 9:00 AM, and the 9:00 bar for dataset "1" is highlighted. Also, the data displayed in the tooltip (1.279) is actually correct for 1AM, not 9.
From what I can tell, this seems to happen because there is a different number of data points in each dataset, and the "index" mode for tooltips doesn't handle this correctly. The data for this chart is below:
{
backgroundColor: "rgba(240, 80, 45, 0.63)",
borderColor: "#f0502d",
label: "1",
data:[
{
"x": 1589497200000,
"y": 0.014
},
{
"x": 1589500800000,
"y": 0.003
},
{
"x": 1589504400000,
"y": 0
},
{
"x": 1589536800000,
"y": 0
},
{
"x": 1589540400000,
"y": 0.023
},
{
"x": 1589544000000,
"y": 0.251
},
{
"x": 1589547600000,
"y": 0.599
},
{
"x": 1589551200000,
"y": 0.896
},
{
"x": 1589554800000,
"y": 1.582
},
{
"x": 1589558400000,
"y": 2.335
},
{
"x": 1589562000000,
"y": 1.302
},
{
"x": 1589565600000,
"y": 2.774
},
{
"x": 1589569200000,
"y": 2.432
},
{
"x": 1589572800000,
"y": 1.257
},
{
"x": 1589576400000,
"y": 0.056
}
]},
{
[
label:"0",
backgroundColor: "rgba(217, 217, 216, 0.63)",
borderColor: "#d9d9d8",
data:{
"x": 1589497200000,
"y": 0.014
},
{
"x": 1589500800000,
"y": 0.003
},
{
"x": 1589504400000,
"y": 0
},
{
"x": 1589536800000,
"y": 0
},
{
"x": 1589540400000,
"y": 0.023
},
{
"x": 1589544000000,
"y": 0.251
},
{
"x": 1589547600000,
"y": 0.599
},
{
"x": 1589551200000,
"y": 0.896
},
{
"x": 1589554800000,
"y": 1.582
},
{
"x": 1589558400000,
"y": 2.335
},
{
"x": 1589562000000,
"y": 1.302
},
{
"x": 1589565600000,
"y": 2.774
},
{
"x": 1589569200000,
"y": 2.432
},
{
"x": 1589572800000,
"y": 1.257
},
{
"x": 1589576400000,
"y": 0.056
}
]}
And the options for this chart: (note that CustomTooltips is just
{
tooltips: {
enabled: true,
intersect: true,
mode: 'index',
position: 'nearest',
},
maintainAspectRatio: false,
legend: {
display: true,
position: 'bottom'
},
scales: {
xAxes: [
{
type:"time",
distribution:"series",
offset:true,
time:{
},
scaleLabel: {
display: true
},
ticks: {
maxRotation: 0,
maxTicksLimit: 12,
}
}],
yAxes: [
{
ticks: {
beginAtZero: true,
maxTicksLimit: 8,
}
}],
},
elements: {
point: {
radius: 0,
hitRadius: 10,
hoverRadius: 4,
hoverBorderWidth: 5,
},
},
}
So my question is: how can I get the tooltips displaying the correct times and correct values?
I could switch the tooltip mode to "x", but then the tooltip only displays for one bar at a time, rather than the bars for both datasets that happen at the same time, so I'd rather not do this.
The cause of your problem may be related to a buggy version of Chart.js. Therefore makek sure to use the latest stable version of the library (currently v2.9.3).
Your code looks fine to me. Nevertheless I made the following slight changes to it.
...removed the elements object from the chart options.
...defined xAxis.time as follows to make sure, the hours in the tick labels and in the tooltip are of the same format.
xAxes: [{
type: "time",
...
time: {
unit: 'hour',
tooltipFormat: 'hA'
},
Please have a look at your amended runnable code below.
const datasets = [{
backgroundColor: "rgba(240, 80, 45, 0.63)",
borderColor: "#f0502d",
label: "1",
data: [{
"x": 1589497200000,
"y": 0.014
},
{
"x": 1589500800000,
"y": 0.003
},
{
"x": 1589504400000,
"y": 0
},
{
"x": 1589536800000,
"y": 0
},
{
"x": 1589540400000,
"y": 0.023
},
{
"x": 1589544000000,
"y": 0.251
},
{
"x": 1589547600000,
"y": 0.599
},
{
"x": 1589551200000,
"y": 0.896
},
{
"x": 1589554800000,
"y": 1.582
},
{
"x": 1589558400000,
"y": 2.335
},
{
"x": 1589562000000,
"y": 1.302
},
{
"x": 1589565600000,
"y": 2.774
},
{
"x": 1589569200000,
"y": 2.432
},
{
"x": 1589572800000,
"y": 1.257
},
{
"x": 1589576400000,
"y": 0.056
}
]
},
{
label: "0",
backgroundColor: "rgba(217, 217, 216, 0.63)",
borderColor: "#d9d9d8",
data: [{
"x": 1589497200000,
"y": 0.014
},
{
"x": 1589500800000,
"y": 0.003
},
{
"x": 1589504400000,
"y": 0
},
{
"x": 1589536800000,
"y": 0
},
{
"x": 1589540400000,
"y": 0.023
},
{
"x": 1589544000000,
"y": 0.251
},
{
"x": 1589547600000,
"y": 0.599
},
{
"x": 1589551200000,
"y": 0.896
},
{
"x": 1589554800000,
"y": 1.582
},
{
"x": 1589558400000,
"y": 2.335
},
{
"x": 1589562000000,
"y": 1.302
},
{
"x": 1589565600000,
"y": 2.774
},
{
"x": 1589569200000,
"y": 2.432
},
{
"x": 1589572800000,
"y": 1.257
},
{
"x": 1589576400000,
"y": 0.056
}
]
}
];
const options = {
tooltips: {
enabled: true,
intersect: true,
mode: 'index',
position: 'nearest',
},
maintainAspectRatio: false,
legend: {
display: true,
position: 'bottom'
},
scales: {
xAxes: [{
type: "time",
distribution: "series",
offset: true,
time: {
unit: 'hour',
tooltipFormat: 'hA'
},
scaleLabel: {
display: true
},
ticks: {
maxRotation: 0,
maxTicksLimit: 12,
}
}],
yAxes: [{
ticks: {
beginAtZero: true,
maxTicksLimit: 8,
}
}],
}
};
new Chart("barChart", {
type: 'bar',
data: {
datasets: datasets
},
options: options
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<canvas id="barChart" height="200"></canvas>

How to merge same key json data into one in c++ using nlhoman json

I have below JSON data:
{
"Created": "2019-08-01T14:36:49Z",
"Tags": [
{
"ObjectId": "1",
"Time": 6,
"TrackerId": "W1"
},
{
"ObjectId": "2",
"Time": 4,
"TrackerId": "E34"
},
{
"ObjectId": "3",
"Time": 4,
"TrackerId": "W1"
},
{
"ObjectId": "4",
"Time": 8,
"TrackerId": "E34"
}
],
"id": 0
}
In the above JSON data, we can see that we have 4 object id's but only 2 tracker id. I need to merge the data which has the same TrackerId and also add their time. So above data will become:
{
"Created": "2019-08-01T14:36:49Z",
"Tags": [
{
"Time": 10,
"TrackerId": "W1"
},
{
"Time": 12,
"TrackerId": "E34"
}
],
"id": 0
}
I am using Nlohmann JSON library for C++. How can we achieve this?
You can build a map of the trackers and then feed them into the JSON object:
json merge_objects(const json& data)
{
std::map<std::string, int> times;
for (const auto& entry : data["Tags"]) {
times[entry["TrackerId"]] += static_cast<int>(entry["Time"]);
}
json result;
result["Created"] = data["Created"];
for (const auto& [id, time] : times) {
json tag;
tag["Time"] = time;
tag["TrackerId"] = id;
result["Tags"].push_back(tag);
}
return result;
}
(live demo)

loopback 3 string not matched with and condition

i am trying to filter with multiple value in loopback 3. 'AND condition' is not working. Numeric value filter working good but string value not matched.
this is my condition is now :
{ "and": [{ "id": { "neq": "5d146f1f7a62651b8162fcf3" } },
{ "gender": "male" }, { "age": { "between": [16, 60] } },
{ "interestId": { "inq": ["5d11c0ce59e2b64e8dc12956", "5d11c0e259e2b64e8dc12958", "5d11c08659e2b64e8dc12953", "5d11c01759e2b64e8dc1294b", "5d11c03759e2b64e8dc1294d"] } },
{ "location": { "near": { "lat": 26.8574194, "lng": 75.7977288 }, "maxDistance": 6, "unit": "kilometers" } },
{ "maritalStatus": { "like": {} } }, { "college": { "like": {} } },
{ "career": { "like": {} } }, { "currentJob": { "like": {} } },
{ "height": { "between": [50, 89] } }, { "weight": { "between": [72, 192] } }, { "bodyBuild": "Slim" }] }
String value is not push in array.
if (interestId.highSchool && interestId.highSchool !="") {
var highschool = new RegExp('.*' + interestId.highSchool + '.*', "i");
query.and.push({
highSchool: { like: highschool }
})
}
where is issue i don't understand. if i did't pass string value it's working fine.
Try following way:-
if (interestId.highSchool && interestId.highSchool !="") {
query.and.push({
highSchool: {regexp: new RegExp('.*' + interestId.highSchool + '.*', "i")}
})
}

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.