T_Ix T_Ix - 1 month ago 16
Javascript Question

Google Postman: How to get properties of an Array [EDIT]

Thx to Florent B. for the helpful answer...

Next Problem is, that I have more than one of the same values in the Array, but it will be tested only once...

Example Output:

{
"#!#": [
{
"foo": "bar",
},
{
"foo": "bar",
},
{
"foo": "bar",
},
{
"foo": "bar",
},
{
"foo": "bar test",
},
{
"foo": "bar test",
}
]
}


Testcode:

var response = JSON.parse(responseBody);
response["#!#"].forEach( function(obj) {
tests[obj.foo] = obj.foo;
});


In my opinion the Test-Result should look like:

Tests(6/6)
PASS - bar
PASS - bar
PASS - bar
PASS - bar
PASS - bar test
PASS - bar test


but it shows only 1 value each:

Tests(2/2)
PASS - bar
PASS - bar test


##################################### OLD PROBLEM

I've got a problem by checking this json properties. I want to test, if the formName is "TestForm_WF1", but I dont know how to write the test to check the properties of this strange named array:

{
"#!#": [
{
"_type": "wfdocument",
"formName": "TestForm_WF1",


normally I got some Output like

[
{
"_type": "application",
...


which I could test with the following forEach-function:

var response = JSON.parse(responseBody);
response.forEach( function(entry) {
test["foo"] = entry._type === "application";
...
});


other way round was that properties have thier own properties and could be tested like:

Output:

{
"license": {
"_type": "license",
...


Testcase:

var jsonData = JSON.parse(responseBody);
tests["bar"] = jsonData.license._type === "license";

Answer

Use a bracket notation for characters that can't be used with a dot notation:

var response = JSON.parse(responseBody);
response["#!#"].forEach( function(entry) {
   test["foo"] = entry._type === "application";
   ... 
});
Comments