Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

query result to JSON object (using javascript action)

Avatar

Avatar
Give Back
Level 3
RaulOcana
Level 3

Likes

14 likes

Total Posts

58 posts

Correct Reply

4 solutions
Top badges earned
Give Back
Validate 25
Validate 10
Validate 1
Boost 5
View profile

Avatar
Give Back
Level 3
RaulOcana
Level 3

Likes

14 likes

Total Posts

58 posts

Correct Reply

4 solutions
Top badges earned
Give Back
Validate 25
Validate 10
Validate 1
Boost 5
View profile
RaulOcana
Level 3

11-06-2019

Hi guys,

It is possible to convert from a query result to a JSON object?

This is the query:

var query = xtk.queryDef.create(

  <queryDef schema="search:country" operation="select">

  <where>

       <condition expr={"@codigo_iata='" + iataCode + "'"}/>

    </where>

  </queryDef>

);

query.SelectAll(false);

result = query.ExecuteQuery();

The result is:

<country-collection>   <country codigo_iata="CO" id_pais="1" id_region="5" nombre_aleman="Kolumbien" nombre_espanol="Colombia" nombre_ingles="Colombia" nombre_portugues="Colômbia"/> </country-collection>

Thank you!

Regards,

Raúl

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Establish
MVP
wodnicki
MVP

Likes

961 likes

Total Posts

1,090 posts

Correct Reply

509 solutions
Top badges earned
Establish
Affirm 500
Contributor
Shape 1
Give Back 100
View profile

Avatar
Establish
MVP
wodnicki
MVP

Likes

961 likes

Total Posts

1,090 posts

Correct Reply

509 solutions
Top badges earned
Establish
Affirm 500
Contributor
Shape 1
Give Back 100
View profile
wodnicki
MVP

12-06-2019

Hi,

You can just define the object by iterating over the result with flatMap or similar, then serialize with JSON.stringify().

Thanks,

-Jon

Answers (3)

Answers (3)

Avatar

Avatar
Validate 1
Level 1
bjoernkoth
Level 1

Likes

2 likes

Total Posts

18 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile

Avatar
Validate 1
Level 1
bjoernkoth
Level 1

Likes

2 likes

Total Posts

18 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile
bjoernkoth
Level 1

27-08-2019

We came up with a helper function like this

/**

* Transforms an ExecuteQuery result into a JS Object resp. JS Object array

* @param {XMLDocument} queryResult a query result

* @param {string} schema the schema name without namespace, e.g. "recipient" instead of "nms:recipient"

* @param {[string]} columns the column names to pull data from and map into the result

* @returns {[object]} an array of JS objects

*/

function queryResultToJS(queryResult, schema, columns) {

    /**

     * Map a given query result item to a JS object

     * @param {object} resultEntry the item mapped to a JS object

     */

    function mapItemToObject(resultEntry) {

        var result = {};

        for (var i = 0; i < columns.length; i++) {

            result[columns[i]] = resultEntry.getAttribute(columns[i]);

        }

        return result;

    }

    var result = [];
    try {

        if (queryResult) {

            if (queryResult.tagName === schema + '-collection') {

                // items in collection or single result

                for each(var collectionItem in queryResult.getElementsByTagName(schema)) {

                    // logInfo('item', JSON.stringify(collectionItem));

                    result.push(mapItemToObject(collectionItem));

                }

            } else if (queryResult.tagName === schema) {

                // single result

                result.push(mapItemToObject(queryResult));

            }

        }

    } catch (error) {

        logError('unable to transform result into JS', error);

    }

    return result;

}

and call it like this

var queryResult = query.ExecuteQuery(),

    deliveries = queryResultToJS(queryResult, "delivery", ["id", "label", "clicks"]);

so basically passing in the columns in the result entries.

The result is always an Array of JS objects, event if the result is just a single entry.

Hope that helps,

Björn

Avatar

Avatar
Springboard
MVP
Florian_Courgey
MVP

Likes

93 likes

Total Posts

87 posts

Correct Reply

19 solutions
Top badges earned
Springboard
Validate 1
Establish
Contributor
Ignite 1
View profile

Avatar
Springboard
MVP
Florian_Courgey
MVP

Likes

93 likes

Total Posts

87 posts

Correct Reply

19 solutions
Top badges earned
Springboard
Validate 1
Establish
Contributor
Ignite 1
View profile
Florian_Courgey
MVP

26-08-2019

Hi,

You can use the following for loops:

var query = NLWS.xtkQueryDef.create(

  <queryDef schema="search:country" operation="select">

  <where>

       <condition expr={"@codigo_iata='" + iataCode + "'"}/>

    </where>

  </queryDef>

);

query.SelectAll(false);

var results = query.ExecuteQuery();

for each(var result in results.getElements()){
  logInfo(result.$id_region);
}

And the following for 1 record:

var query = NLWS.xtkQueryDef.create(

  <queryDef schema="search:country" operation="get">

  <where>

       <condition expr={"@codigo_iata='" + iataCode + "'"}/>

    </where>

  </queryDef>

);

query.SelectAll(false);

var result = query.ExecuteQuery();

logInfo(result.$id_region);

Regards

Avatar

Avatar
Validate 1
Level 1
bjoernkoth
Level 1

Likes

2 likes

Total Posts

18 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile

Avatar
Validate 1
Level 1
bjoernkoth
Level 1

Likes

2 likes

Total Posts

18 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile
bjoernkoth
Level 1

21-08-2019

Hi,

do we have a working example for this? I wonder why this is not in the basic toolset of AC.

Cheers