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

View Entire Topic

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