0

Performing a GET request that returns in XML format. I converted that to JSON as outlined below. I am looking to parse an array and set an array as a env. variable for each specific object in the array.

How can I achieve looping through Tuple.Answer to get all highlighted values for each object from the response? With the current test scripts I am getting the following in my ActionIDs env variable:[“1965555”,“1965555”,“1965555”,“1965555”,“1965555”,“1965555”]

Path to data I want to retrieve:

BESAPI.Query.Result.Tuple.Answer[0]._

Here is my action script:

var jsonData = xml2Json(responseBody);

var ActionIDs = ;

.each(jsonData.BESAPI.Query.Result.Tuple[0].Answer[0]., () => {
  ActionIDs.push(jsonData.BESAPI.Query.Result.Tuple[1].Answer[0]._)
})

pm.environment.set(“ActionID”, ActionIDs.shift());

pm.environment.set(“ActionIDs”, JSON.stringify(ActionIDs));

pm.test(“Status code is 200”, function() {
  console.log(pm.response.code);
  pm.response.to.have.status(200);

});

Response:

    <?xml version="1.0" encoding="UTF-8"?>
<BESAPI xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="BES.xsd">
    <Query Resource="(id of it, name of it, state of it) of bes actions">
        <Result>
            <Tuple>
                <Answer type="integer">1965354</Answer>
                <Answer type="string">Test Policy 2: Test Policy Azure Test: (1/4)</Answer>
                <Answer type="string">Open</Answer>
            </Tuple>
            <Tuple>
                <Answer type="integer">1965555</Answer>
                <Answer type="string">Test Policy 2: Test Policy Azure Test: (2/4)</Answer>
                <Answer type="string">Open</Answer>
            </Tuple>
            <Tuple>
                <Answer type="integer">1965761</Answer>
                <Answer type="string">Test Policy 2: Test Policy Azure Test: (3/4)</Answer>
                <Answer type="string">Open</Answer>
            </Tuple>
            <Tuple>
                <Answer type="integer">1965962</Answer>
                <Answer type="string">Test Policy 2: Test Policy Azure Test: (4/4)</Answer>
                <Answer type="string">Open</Answer>
            </Tuple>
            <Tuple>
                <Answer type="integer">1967153</Answer>
                <Answer type="string">Image: Windows Images: (1/6)</Answer>
                <Answer type="string">Open</Answer>
            </Tuple>
            <Tuple>
                <Answer type="integer">1967354</Answer>
                <Answer type="string">Image: Windows Images: (2/6)</Answer>
                <Answer type="string">Open</Answer>
            </Tuple>
            <Tuple>
                <Answer type="integer">1967555</Answer>
                <Answer type="string">Image: Windows Images: (3/6)</Answer>
                <Answer type="string">Open</Answer>
            </Tuple>
            <Tuple>
                <Answer type="integer">1967756</Answer>
                <Answer type="string">Image: Windows Images: (4/6)</Answer>
                <Answer type="string">Open</Answer>
            </Tuple>
            <Tuple>
                <Answer type="integer">1967957</Answer>
                <Answer type="string">Image: Windows Images: (5/6)</Answer>
                <Answer type="string">Open</Answer>
            </Tuple>
            <Tuple>
                <Answer type="integer">1968158</Answer>
                <Answer type="string">Image: Windows Images: (6/6)</Answer>
                <Answer type="string">Open</Answer>
            </Tuple>
        </Result>
        <Evaluation>
            <Time>8.844ms</Time>
            <Plurality>Plural</Plurality>
        </Evaluation>
    </Query>
</BESAPI>
Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
Sling4778
  • 21
  • 4
  • Could you reformat the code in the question and also add the actual response body and not an image, please. It will help people to replicate it locally and not have to type it all out again. – Danny Dainton Apr 28 '20 at 20:10
  • @DannyDainton I formatted the original post. Appreciate any assistance! – Sling4778 Apr 29 '20 at 03:09
  • I am pretty sure the error lies within the following line of code: .each(jsonData.BESAPI.Query.Result.Tuple[0].Answer[0]., () => { ActionIDs.push(jsonData.BESAPI.Query.Result.Tuple[1].Answer[0]._) As my ActionIDs variable is being populated repeatedly with "1965555", which is the value of jsonData.BESAPI.Query.Result.Tuple[1].Answer[0]._ I have to be overlooking something very simple or have a line of code incorrect. End goal is to parse the Tuple array for each .Answer[0]._ value and set it as the ActionIDs variable in a stringified format – Sling4778 Apr 29 '20 at 03:19

1 Answers1

0

Something like this should store those Ids as an environment variable:

let jsonObject = xml2Json(responseBody),
    ActionIDs = [];

_.each(jsonObject.BESAPI.Query.Result, (arrItem) => {
    _.each(arrItem, (result) => {
        ActionIDs.push(result.Answer[0]["_"])
    })
})

pm.environment.set("ActionIDs", JSON.stringify(ActionIDs));
Danny Dainton
  • 23,069
  • 6
  • 67
  • 80