MQL4 JSON Parsing

 

Hi, 

I am kind of new to JSON in mql4. But i had an idea i wanted to try send some indicator data to a google docs spreadsheet.

I have made the Google Apps Script, and when i send the data from postman it updates perfect in my spreadsheet so i know my problem is within MQL4.

When i run my expert advisor i get the message below which i dont understand, so if someone can tell me what i am doing wrong it would be much appreciated. :)

Here is the code i use to build the parsing:

// Funktion til at sende data til Google Sheets
void SendToGoogleSheets(string symbol, double adx, double plusDI, double minusDI) {
    // Byg JSON-dataen
    
    adx = DoubleToString(adx, 1); // Begræns til 1 decimal
    plusDI = DoubleToString(plusDI, 1); // Begræns til 1 decimal
    minusDI = DoubleToString(minusDI, 1); // Begræns til 1 decimal
    
    string postData = "{\"symbol\":\"" + symbol + "\"," + "\"adx\":" + DoubleToString(adx, 1) + "," + "\"plusDI\":" + DoubleToString(plusDI, 1) + "," + "\"minusDI\":" + DoubleToString(minusDI, 1) + "}";
           
    Print("Manuelt opbygget JSON: ", postData);
    // Definer arrays til WebRequest
    char data[], result[];
    string resultHeaders;
    // Konverter JSON-data til char-array
    StringToCharArray(postData, data);
    // Udfør POST-forespørgsel
    int res = WebRequest(
        "POST",                               // HTTP-metode
        url,                                  // Google Apps Script URL
        "Content-Type: application/json\r\n", // HTTP-headers
        10 * 1000,                            // Timeout på 10 sekunder
        data,                                 // JSON-data som body
        result,                               // Resultat-buffer
        resultHeaders                         // Resultat-headers
    );
    // Håndter responsen
    if (res == -1) {
        Print("Fejl ved WebRequest: ", ErrorDescription(GetLastError()));
    } else {
        string response = CharArrayToString(result); // Konverter respons til string
        Print("Google Sheets Response: ", response);
    }
}

Improperly formatted code edited by moderator. Please always use the CODE button (Alt-S) when inserting code.

Code button in editor

 
Søren Lauritsen:


    // Konverter JSON-data til char-array

    StringToCharArray(postData, data);

The last character is string terminator '\0'

Just constraint the resulting array to  ArraySize(data) - 1 or StringLen(postData).

ArrayResize(data, ArraySize(data) - 1);
 
Samuel Manoel De Souza #:

The last character is string terminator '\0'

Just constraint the resulting array to  ArraySize(data) - 1 or StringLen(postData).

Thanks alot, your are the best :)