Postman - How can I pass array as variable - postman

Is there the possibility to use an array variable inside postman?
e.g. inside the body of a request:
{
"myData" : {{arrayVariable}}
}
and inside the data file:
{
"arrayVariable": ["1", "2", "3"]
}

It's possible, you can even add your own keys

You can create a JSON body like this:
{
"myData" : [
{{arrayVariable}}
]
}
And the variable like this:
arrayVariable: "1", "2", "3"
where arrayVariable is the key and "1", "2", "3" is the value.

using variable with a same name will give you an array

Postman environment variables are meant to just save data as string, so here you are the workaround to pass array as environment variable/data file to Postman as a string like this:
{
"arrayVariable": '["1", "2", "3"]'
}
Then, add the following piece of code to parse this variable in pre-request script in Postman like this:
var x = JSON.parse(postman.getEnvironmentVariable("arrayVariable"));
postman.setEnvironmentVariable("arrayVariable", x);

Please create your body request like below
{
"myData" : ["{{arrayVariable}}"]
}
and there is no change required for data file.you can use as it is.
{
"arrayVariable": ["1", "2", "3"]
}
It will work definatly.

The only solution worked for me was something like the answer MickJagger provided, but I think it needs some clarifications.
The JSON data file should be something like this:
[
{
"anArray": "1, \"2\", 3.0, \"Foo\", false"
}
]
which it's value is a string, escaping the quotations for string elements.
(Note that this example differs from example provided by original question, to cover more use cases.)
The variables is as MickJagger said:
{
"value": [{{anArray}}]
}
Maybe other solutions works on previous postman versions, but this solution is tested on postman's latest version (by the time of publishing this answer), i.e. v7.34.0 .

Related

Cannot convert std::string to QJsonArray in Qt

The following text is a bit of std::string text that is generated by another app (I do not have control of what the app sends me). I have tried for days to get this converted into a QJsonArray and cannot figure this out. I am using C++ within QT. Does anyone have a bit of direction or sample C++ code that could solve this?
{
"saved_mik_yous": {
"2120ce2d-a5b1-49b8-8384-3781b7b2d73b": {
"name": null,
"id": "2120ce2d-a5b1-49b8-8384-3781b7b2d73b",
"start": 1565288936.1127193,
"end": 1565289128.1236603,
"mixxer": 128.567505,
"mik_source": "algo"
},
"bf855c0d-a71d-42ea-b3ef-7cbe0e2c7a3d": {
"name": null,
"id": "bf855c0d-a71d-42ea-b3ef-7cbe0e2c7a3d",
"start": 1565301673.4609745,
"end": 1565301832.665656,
"mixxer": 308.485107,
"mik_source": "algo"
}
},
"mik_you_state": "completed"
}
All you have to do is this:
QJsonDocument doc = QJsonDocument::fromJson(QByteArray::fromStdString(str));
Then, you can access the values for the keys for example as:
doc["saved_mik_yous"]
And so on.
Mind you, the json you are showing seems to be an object rather than an array since it contains key-value pairs rather than a list of elements inside square brackets. So, whilst it does not matter when you are converting the std::string into a QJsonDocument, you need to access the values by keys rather than indices.
If you are getting dynamic json which can be either an array or object, you can always check for the type with isArray() or isObject() to convert it to the right type.

Postman- Use collection variable in request body

I have a problem with Postman, where I want to use collection variables inside the request body.
According to postman documentation, all variables in postman GUI can be retrieved with double curly braces {{}}.
But it does not work for me. If I move variables from collection to environment, everything is working OK, but as soon as I move the variable from the environment to collection, it starts throwing errors like this:
JSONError: Unexpected token 'U' at 1:1
Unrecognized token 'Backend': was expecting (JSON String, Number, Array, Object
This is my body:
{
"name": {{BackendValidationPSName}},
"groups": {{myBackendValidationRGuuids}}
}
Can anyone point me in the right direction? Tx.
The values have to be in double quotes
{
"name": "{{BackendValidationPSName}}",
"groups": "{{myBackendValidationRGuuids}}"
}
Solved. I was missing the "" in the collection variable value.

How to store arraylist in global variable and call it in another request

My current Response is in arraylist
“pinvalues”:
[
1,
3
]
i want to store this 2 value in global variable then i need to call this in another request like below.
Request
pinposition":
[
1,
3
]
how can i achieve it,Any suggestion , Advance thank you for your help.
In your first request in Tests scripts, you can parse and save the array as a string:
const response = pm.response.json();
pm.environment.set('pinvalues', JSON.stringify(response.pinvalues));
In your another request, you set the following body:
{
"pinposition": {{pinvalues}}
}

Create / Update multiple objects from one API response

all new jsfiddle: http://jsfiddle.net/vJxvc/2/
Currently, i query an api that will return JSON like this. The API cannot be changed for now, which is why I need to work around that.
[
{"timestamp":1406111961, "values":[1236.181, 1157.695, 698.231]},
{"timestamp":1406111970, "values":[1273.455, 1153.577, 693.591]}
]
(could be a lot more lines, of course)
As you can see, each line has a timestamp and then an array of values. My problem is, that i would actually like to transpose that. Looking at the first line alone:
{"timestamp":1406111961, "values":[1236.181, 1157.695, 698.231]}
It contains a few measurements taken at the same time. This would need to become this in my ember project:
{
"sensor_id": 1, // can be derived from the array index
"timestamp": 1406111961,
"value": 1236.181
},
{
"sensor_id": 2,
"timestamp": 1406111961,
"value": 1157.695
},
{
"sensor_id": 3,
"timestamp": 1406111961,
"value": 698.231
}
And those values would have to be pushed into the respective sensor models.
The transformation itself is trivial, but i have no idea where i would put it in ember and how i could alter many ember models at the same time.
you could make your model an array and override the normalize method on your adapter. The normalize method is where you do the transformation, and since your json is an array, an Ember.Array as a model would work.
I am not a ember pro but looking at the manual I would think of something like this:
a = [
{"timestamp":1406111961, "values":[1236.181, 1157.695, 698.231]},
{"timestamp":1406111970, "values":[1273.455, 1153.577, 693.591]}
];
b = [];
a.forEach(function(item) {
item.values.forEach(function(value, sensor_id) {
b.push({
sensor_id: sensor_id,
timestamp: item.timestamp,
value: value
});
});
});
console.log(b);
Example http://jsfiddle.net/kRUV4/
Update
Just saw your jsfiddle... You can geht the store like this: How to get Ember Data's "store" from anywhere in the application so that I can do store.find()?

jqgrid dropdown select editoptions

I am trying to use the edittype:"select", formatter:"select" and editoptions:{values:'1:Type1;2:Type2'} in my colModel
colModel : [
{name:'pk', index:'pk', width:20, sortable:true, jsonmap:'pk',
sorttype:'integer'},
{name:'id', index:'id', align:'left', jsonmap:'fields.id',
sortable:true, editable:true, edittype:'select', formatter:'select',
editoptions:{value:'1:value1;2:value2;3:value3'},
{name:'type', index:'type', width:100,align:'center',
jsonmap:'fields.type', sortable:true,editable:true}
]
but the value for id returned in the json object is not a string (it doesn't have quotes around it). If I remove the edittype and editoptions the id value appears in the column of the grid but when I include the edittype, formatter and editoptions in the colMode definition I get the javascript error
(E||"").replace is not a function
The json object that fails looks like
{ "pk": 120
"model": "myModel"
"fields": {
"id": 1,
"type": "aType"
}
}
The id value has no quotes.
I am using the edittype, formatter and editoptions in other grids but the value I am macthing against is a character (in the json object it is surrounded by quotes) and it works perfectly.
I am only guessing that the problem is with the unquoted number but I am not sure. Has anyone seen this before?
Regards
Andrew
Ok,
I found that the problm is on line 1067 of the jquery.1.3.2 file. It is the trim function and the code looks like this:
trim:function(text) {
return (text||"").replace(/^\s+|\s+$/g, "")
}
I changed it to this:
trim:function(E) {
return (text.toString()||"").replace(/^\s+|\s+$/g, "")
}
and now it works.
Can anyone tellme if this is a bug or is there something else I can do to overide ths function without changing the jquery file.
Function Override: Placed in the head of any page that shows this problem.
$.extend({
trim:function(E) {
return (text.toString()||"").replace(/^\s+|\s+$/g, "")
}
});
Thanks
Andrew