Best JSON array representation c++ - c++

Before i have this json body:
{
"users": [{
"userId": "userId1"
},
{
"userId": "userId2"
}
]
}
This body is send by my java client (its not matter for this case) and i receive in my c++ server with one list.I iterate the list and get all user id from request body.
List example: std::list<UserId>* pUsers;
But now i want to improve and make more easy my body request, so i want an body request like this:
{
"users": {
"userId": ["userId1", "userId2"]
}
}
Note: I only put two users but it can be many of users. I'm new with c++ so i have some difficulties. What is the best representation to do this on c++? Its is one list too? Its a vector? Its a array of strings? Someone can help me and show me some examples or explain to me the best solution?
Thanks.

Related

How to convert array of object(hasMany relationship data) to array of id?

I wanted to get data and show it in UI. Here's how i write to get the "movies" data.
let movies= yield this.store.findAll('movie');
And I log the "movies". As the picture below shows that there's no data for "photos".
Here's the network:
I'm getting data back from hasura like this:
{
"data": {
"movies": [
{
"id": "584db434-5caa-475e-b3ec-e98e742f0030",
"movieid": "abc123",
"description": "Penquins dancing in antactica",
"photos": [
{
"id": "c4d2833a-4896-42b0-ae8b-0ab9fe71d1d4"
},
{
"id": "e04697e3-21fe-4f0e-8012-443f26293340"
}
]
}
]
}
}
But Ember.js can't read and render the relationship data (photos). Is it the "photos" data should be like this?
"photos": [c4d2833a-4896-42b0-ae8b-0ab9fe71d1d4, e04697e3-21fe-4f0e-8012-443f26293340]
How can I convert it in Ember? or in Hasura?
Thanks for updating your question!
Since you're using ember-data, you'll need a custom adapter and serializer to form your data into the format that ember-data is expecting (since there are infinite numbers of ways APIs decide how to structure data).
More information on that can be found here:
https://guides.emberjs.com/release/models/customizing-adapters/
and here: https://guides.emberjs.com/release/models/customizing-serializers/
Your data is fairly well structured already, so conversion should hopefully go well. Comment back if you have issues <3

Postman adding values to request json

Hi I am hoping this is a simple question.
In my pre-request-script I am getting a JSON object back from a GET.
This JSON object has 10 fields. I would like to add 2 more.
I tried myJson.add and myJson.push but those don't work. How would I accomplish this task? I am then taking that myJson and adding it to a push request in the test.
Thanks in Advance
With the lack of data in the description, I'm providing a very general answer
Assuming myJson contains your JSON string, first parse it to convert the JSON data to an object as follows:
let jsonObj = JSON.parse(myJson);
Once done, now you can add/remove/update the data - depending on the structure of your JSON.
For example, assuming your data is an array:
[
{
"data": "value"
},
{
"data": "value2"
}
]
You can add another element by using:
jsonObj.push({"data": "value3"});
Once you are done updating the data, convert it back to string as follows:
myJson = JSON.stringify(jsonObj);
You can now store this in an environment variable etc for use in the Postman request.
Reference: https://learning.postman.com/docs/sending-requests/variables/

Tests and Environments

i'm newbie here and newbie also using postman.
I'm trying to use environments to store values from responses and use them to the next request. I found some examples on the web and i use them.
I managed to store the first value in a environment but not the 2nd, 3rd, in the same request. I tried many different ways to write the tests but without success
My tests' code is this:
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("clientID", jsonData.LoginClientID);
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("COMPANY", jsonData.objs.data[0].LoginCompan
Response was this:
{
"success": true,
"clientID": "abcd",
"objs": [
{
"COMPANY": "1",
"BRANCH": "1",
"MODULE": "0",
}
],
"ver": "5.00.518.11143"
}
Running the POST request the value of clientID is stored in enviroment's value but not COMPANY
Any advice ?
Thanks Eddie
Just remove the data part of the code when setting the variable. You're just after the list of items in that objs array, not sure where data comes into it.
For example, from your code:
jsonData.objs[0].LoginCompan
More information about extracting values from a response body can be found here:
https://community.getpostman.com/t/sharing-tips-and-tricks-with-others-in-the-postman-community/5123/5

Postman Printing specific information from Response

I'm using Postman's console to display the response of the API Call with console.log, I'm using the runner since I have a lot of iterations. However, a lot of information from the API response are giving me trouble, so I would like to do is to print with console.log specific information of the responseBody.
As test with Postman, I'm using the following:
var body = JSON.parse(responseBody);
console.log(JSON.stringify(body.data));
The response is:
[{"device":"1BED7","time":1505320342,"data":"05b006bcac00000000000000","snr":"21.00","linkQuality":"AVERAGE","seqNumber":555,"rinfos":[{"tap":"A2A","delay":1.4,"lat":"53.0","lng":"2.0"},{"tap":"A2B","delay":0.5,"lat":"53.0","lng":"2.0"}]},{"device":"1CED7","time":1505277142,"data":"05b006bcac00000000000000","snr":"20.68","linkQuality":"AVERAGE","seqNumber":554,"rinfos":[{"tap":"A2C","delay":1.3,"lat":"53.0","lng":"2.0"},{"tap":"232","delay":1.9,"lat":"53.0","lng":"2.0"}]},{"device":"152C3","time":1505233937,"data":"05b006bcac00000000000000","snr":"19.14","linkQuality":"AVERAGE","seqNumber":553,"rinfos":[{"tap":"215","delay":2.4,"lat":"53.0","lng":"2.0"}]},{"device":"1BF81","time":1505190735,"data":"05b006bcac00000000000000","snr":"21.67","linkQuality":"AVERAGE","seqNumber":552,"rinfos":[{"tap":"1CC","delay":2.0,"lat":"53.0","lng":"2.0"},{"tap":"25A","delay":1.6,"lat":"53.0","lng":"2.0"}]},
What I would want to print with console.log would be only the values of device, time and data:
{"1BED7",1505320342,"05b006bcac00000000000000"},{"1CED7",1505277142,"05b006bcac00000000000000"},{"152C3",1505233937,"05b006bcac00000000000000"},
and so on...
My programming skills are very limited so sorry if the answer is so obvious, I have tested a lot of things but I'm still stuck.
Thanks a lot if you can help
I think your your response is array of objects.It is already a json object.So firstly what you are doing wrong is you don't need to parse it.You can directly use it.Check this below code snippet at the end of the answer.I think this satisfies your need.I used the forEach function to iterate through the the response array and push value that you need into the empty array.This result array contains object in following format.you can access each property of each object of this array by javascript . operator.I think that is quite obvious to you.
[
{
"device": "1BED7",
"time": 1505320342,
"data": "05b006bcac00000000000000"
},
{
"device": "1CED7",
"time": 1505277142,
"data": "05b006bcac00000000000000"
},
{
"device": "152C3",
"time": 1505233937,
"data": "05b006bcac00000000000000"
},
{
"device": "1BF81",
"time": 1505190735,
"data": "05b006bcac00000000000000"
}
]
var responseBody=[{"device":"1BED7","time":1505320342,"data":"05b006bcac00000000000000","snr":"21.00","linkQuality":"AVERAGE","seqNumber":555,"rinfos":[{"tap":"A2A","delay":1.4,"lat":"53.0","lng":"2.0"},{"tap":"A2B","delay":0.5,"lat":"53.0","lng":"2.0"}]},
{"device":"1CED7","time":1505277142,"data":"05b006bcac00000000000000","snr":"20.68","linkQuality":"AVERAGE","seqNumber":554,"rinfos":[{"tap":"A2C","delay":1.3,"lat":"53.0","lng":"2.0"},{"tap":"232","delay":1.9,"lat":"53.0","lng":"2.0"}]},{"device":"152C3","time":1505233937,"data":"05b006bcac00000000000000","snr":"19.14","linkQuality":"AVERAGE","seqNumber":553,"rinfos":[{"tap":"215","delay":2.4,"lat":"53.0","lng":"2.0"}]},{"device":"1BF81","time":1505190735,"data":"05b006bcac00000000000000","snr":"21.67","linkQuality":"AVERAGE","seqNumber":552,"rinfos":[{"tap":"1CC","delay":2.0,"lat":"53.0","lng":"2.0"},{"tap":"25A","delay":1.6,"lat":"53.0","lng":"2.0"}]}];
var array=[];
responseBody.forEach(function (obj) {
array.push({device:obj.device,time:obj.time,data:obj.data})
})
console.log(array);
let results = _.map(JSON.parse(responseBody),
(sensor) => { return [sensor.device, sensor.time, sensor.data]});
// results contains an array like
// [[deviceId1, time1, data1], [deviceId1, time1, data1], ...]
console.log(results);

How to write CouchDb views?

I have list of such documents in my database of couchDB.
{
"_id": "9",
"_rev": "1-f5a9a0b76c6ae1fe5e20f1a1f9e6f8ba",
"Project": "Vaibhava",
"Type": "activity",
"Name": "Civil_Clearence",
"PercentComplete": "",
"DateAndTime": "",
"SourcePMSId": "1049",
"ProgressUpdatedToPMSFlag": "NO",
"UserId": "Kundan",
"ParentId": "5"
}
How to write a view function so that when i pass a doc._id as a key then i must get all siblings of that doc._id(docs with ParentId same as the key which I have sent)??
As said in another answer, it is not possible to do that with a single request.
However, you can do the following instead:
Define a map (with no reduce) view indexed on ParentID:
function(o) {
if (o.ParentID) {
emit(o.ParentID);
}
}
Send a first request to your object to know the ID of its parent:
GET /myDatabase/myObject
Then send a request to your view
GET /myDatabase/_design/myApp/_view/myView/?key="itsParent"&include_docs=true
Having several requests should not cause much harm here, since their number (2) is constant.
Moreover you can hide them behind a single request handled by NodeJS.
Unfortunately, you would need to chain together two map-reduce functions to achieve this result and that functionality is not available in CouchDB. See this question for further information.