Javascript Input and Output

I am trying to use AmiQuote's "Run Javascript to process data" feature for the first time. As a debugging step, I am using the default implementation of the Process() function:

// the processing function takes text as input and produces text as output
function Process( input )
{
	return input;
}

On the Debug tab of the User-Defined Data Source, I see:

It appears that all of the line termination characters have been stripped out of the Input variable before the Process() function was called. Is this the expected behavior, and if so, is there a way to override it?

No, it doesn't strip but your data source might be using \r instead of \n

See:


function Process(input)
{
 return "Test\nTest";
}

as opposed to:

function Process(input)
{
 return "Test\rTest";
}

In fact Windows edit control requires a pair \r\n to display text in actual new line.

1 Like

OK, I can try to check that. But if the data source is only using /r as a line terminator, then wouldn't the input shown in the Response control all be on a single line as well, just like the output?

One other thing I should mention. I can successfully import price data (but no full name, which is ultimately what I'm after) with this API if I leave AmiQuote's "Run Javascript to process data" option deselected. But if I select that option and use the default JavaScript which simply returns the Input field (shown in the original post), then the price import will fail.

I have confirmed that the data source terminates lines with \r\n (Hex 0D0A). Here is an API response captured with Postman:

00000000: 4D 53 46 54 0D 0A 4D 69 63 72 6F 73 6F 66 74 0D    MSFT..Microsoft.
00000010: 0A 44 61 74 65 2C 41 64 6A 75 73 74 65 64 20 50    .Date,Adjusted P
00000020: 72 69 63 65 2C 4F 70 65 6E 2C 4C 6F 77 2C 48 69    rice,Open,Low,Hi
00000030: 67 68 2C 56 6F 6C 75 6D 65 0D 0A 30 32 2F 31 34    gh,Volume..02/14
00000040: 2F 32 30 32 35 2C 34 30 37 2E 36 31 37 30 2C 34    /2025,407.6170,4
00000050: 30 36 2E 39 37 37 30 2C 34 30 35 2E 30 36 35 30    06.9770,405.0650
00000060: 2C 34 30 38 2E 31 30 32 30 2C 32 32 37 35 38 2E    ,408.1020,22758.
00000070: 34 36 30 30 0D 0A 30 32 2F 31 38 2F 32 30 32 35    4600..02/18/2025
00000080: 2C 34 30 38 2E 38 32 35 30 2C 34 30 37 2E 31 38    ,408.8250,407.18
00000090: 36 30 2C 34 30 35 2E 36 38 34 30 2C 34 30 39 2E    60,405.6840,409.
000000a0: 37 38 36 30 2C 32 31 34 32 33 2E 30 35 30 30 0D    7860,21423.0500.
000000b0: 0A 30 32 2F 31 39 2F 32 30 32 35 2C 34 31 33 2E    .02/19/2025,413.
000000c0: 39 34 34 30 2C 34 30 37 2E 30 36 36 30 2C 34 30    9440,407.0660,40
000000d0: 36 2E 38 33 31 30 2C 34 31 34 2E 36 36 39 30 2C    6.8310,414.6690,
000000e0: 32 34 31 31 34 2E 32 30 30 30 0D 0A 30 32 2F 32    24114.2000..02/2
000000f0: 30 2F 32 30 32 35 2C 34 31 36 2E 31 33 30 30 2C    0/2025,416.1300,
00000100: 34 31 35 2E 32 39 30 30 2C 34 31 32 2E 35 34 30    415.2900,412.540
00000110: 30 2C 34 31 39 2E 33 31 30 30 2C 32 33 35 30 38    0,419.3100,23508
00000120: 2E 37 33 30 30 0D 0A 30 32 2F 32 31 2F 32 30 32    .7300..02/21/202
00000130: 35 2C 34 30 38 2E 32 31 30 30 2C 34 31 37 2E 33    5,408.2100,417.3
00000140: 33 35 30 2C 34 30 37 2E 38 39 30 30 2C 34 31 38    350,407.8900,418
00000150: 2E 30 34 38 30 2C 32 37 35 32 34 2E 38 30 30 30    .0480,27524.8000
00000160: 0D 0A       

I modified the JScript in AmiQuote to write the input data to a file without doing any modification:

// the processing function takes text as input and produces text as ouput
function Process(input)
{
    // Create an instance of the FileSystemObject    
    var fso = new ActiveXObject("Scripting.FileSystemObject");    

    // Open the text file for writing    
    var file = fso.OpenTextFile("C:\\Temp\\Input.txt", 2, true);    

	// Write the input to the text file    
	file.Write(input);    

	// Close the text file    
	file.Close

    return input;
}

Using a hex editor from VS Code, I confirmed that the output file (which is the input passed to my JScript by AmiQuote) has no line termination characters:

This is consistent with what I see in the AmiQuote Debug window: no line terminators in the Processed Content (which in fact is unmodified).

I am using 64-bit AmiQuote v4.17, which I believe is the most recent version. It still seems to me that the most likely explanation for the behavior that I'm seeing is that AmiQuote has removed the \r\n characters from the API response. I'm happy to look into other possibilities if you have any suggestions.

Run the code I provided EARLIER:

function Process(input)
{
 return "Test\r\nTest2\r\nTest3";
}

It displays new line in processed content.

Also, if your data source already returns CSV , why do you run Javascript AT ALL?

It is NOT needed when you already have CSV.

Javascript is needed IF and ONLY IF, data server returns JSON.

There might be some misunderstand why do you need Javascript at all. New lines in JSON are IGNORED because JSON should escape them. New lines might be removed from 'input' variable (as it assumes that new lines are escaped), but they are NOT removed in "Processed content" field and NOT removed from files that AmiQuote generates as a result of Process() function call.
If you are NOT using JSON, there is no point in using Javascript at all.

Using this API, I can successfully import price data if I leave AmiQuote's "Run Javascript to process data" option deselected. So yes, the CSV data can be processed directly by AmiQuote.

My goal is to retrieve the full name of the ticker from the second line of the API response. To do that, I was hoping to utilize AmiQuote's Javascript capability to process the full name and then allow AmiQuote to process all the price data. However, as soon as I select the Javascript option, then the price import will fail even if I've done nothing to modify the input that was passed to the Process function. To me, this clearly indicates that AmiQuote has modified the API data before it reaches the Process() function.

To prevent breaking any existing AmiQuote Data Source definitions, could you provide an option that would prevent AmiQuote from doing any preprocessing of the data retrieved from the API before passing that data to the Process() function? I realize that having the data provider return JSON or XML would be a more robust solution, but so far they have not been willing to do that.

@Tomasz: Any further thoughts on being able to pass the unmodified API result from the data provider to the AmiQuote Javascript function, or should I just build my own solution outside of AmiQuote?

Next version will leave single \n at the end of the line but will trim other leading/trailing white spaces (that is required for other reasons).

Perfect! Any idea when that new version might be available?

The release is planned this week

2 Likes