Skip to main content
Skip table of contents

Converters

Converters are pieces of JavaScript code that take the result from a query (and optionally metadata), process it into a custom array of objects and then pass it on to the template. You can also perform arbitrary calculation, formatting and renaming.. Converters are mainly required for JSON responses from REST APIs, but they can be applied to SQL results as well.

Properties

Each converter consists of the following properties:

  • Name: The Name identifying the converter.

  • Content: JavaScript code that receives the query result and returns an array of objects. Use the text link on the top right above the input field to insert the scaffold. In most cases, you will only need to adjust the code within the method call result.push(), where you walk through the result line by line and extract the information you need.

For various examples on how to use converters, refer to Full REST API Examples.

Converters may optionally receive a second object as argument that contains the following data:

  • queryName (String)the name of the query currently executing this converter

  • queryParameters (Object): the parameters of the query currently executing this converter

  • originalQueryParameters (Object): same as queryParameter but before any processing took place (e.g. wildcard replacements)

Also see the last example below for more information how to use the second argument.

Example Converters

Converter scaffold:

CODE
// This Converter assumes that the JSON from the Query looks like this:
// { items: [{a: 1, b: 2}, ...] }
// It extracts relevant data out of the items array.
function convert(json) {
	// Parse the JSON into an usable object.
	var parsed = JSON.parse(json);

	// Iterate over all objects in the 'items' array.
	return parsed.items.map(function(item) {
		// Extract the data that we need from each 'item'.
		return {
			'Column A': item.a,
			'Column B': item.b
		};
	});
}

Example converter for a Facebook REST call that fetches all pages liked by the user:

JS
// This Converter assumes that the JSON from the Query looks like this:
// { items: [{a: 1, b: 2}, ...] }
// It extracts relevant data out of the items array.
function convert(json) {
	// Parse the JSON into an usable object.
	var parsed = JSON.parse(json);

	// Iterate over all objects in the 'items' array.
	return parsed.items.map(function(item) {
		// Extract the data that we need from each 'item'.
		return {
            'Page name': item.name,
            'Date of pressing Like': PocketQuery.formatDate(item.created_time,'YYYY-MM-DD (dddd), hh:mm') + ' Uhr'
        };
	});
}

Example converter using metadata:

JS
function convert(json, metadata) {
    return [{
        "QueryName": metadata.queryName,
        "QueryParameter Continent": metadata.queryParameters.Continent,
        "QueryParameter MinPopulation": metadata.queryParameters.MinPopulation,
        "Original QueryParameter Continent": metadata.originalQueryParameters.Continent,
        "Original QueryParameter MinPopulation": metadata.originalQueryParameters.MinPopulation
    }];
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.