Failed to decode JSON object Error

 
I have a JSON string that I'm trying to use to make a webrequest
string json = "{\r\n"
                "\"time\": \"2023-06-06 10:00:00\",\r\n"
                "\"type\": 2,\r\n"
                "\"volume\": 0.02,\r\n"
                "\"stoploss\": 1.2000,\r\n"
                "\"takeprofit\": 1.2500,\r\n"
                "\"price\": 1.2200\r\n"
            "}";

 // Convert the JSON string to a char array
 
   int charArraySize = StringLen(json) + 1; // Add 1 for null terminator
   ArrayResize(jsonData, charArraySize);
   StringToCharArray(json, jsonData, charArraySize, WHOLE_ARRAY, CP_UTF8);

  // Convert the uchar array to a string
  
   string jsonString = CharArrayToString(jsonData,0,WHOLE_ARRAY,CP_UTF8);
   Print("<< >> \n",jsonString);

When, Print to see how it looks like. I get 

<!doctype html>
<html lang=en>
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>Failed to decode JSON object: &#39;utf-32-{
"time": "2023-06-06 10:00:00",
"type": 2,
"volume": 0.02,
"stoploss": 1.2000,
"takeprofit": 1.2500,
"price": 1.2200
}

To be specific, it says 

<p>Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)</p>

I have tried to change json format and fix a line or two, but nothing seems to change

 
Omega J Msigwa:
I have a JSON string that I'm trying to use to make a webrequest

When, Print to see how it looks like. I get 

I have tried to change json format and fix a line or two, but nothing seems to change

Sorry but your post doesn't make sense. You posted code which is not related to the log you posted. Also you said it failed to decode but there is no decode statement in your code.
 
Alain Verleyen #: Sorry but your post doesn't make sense. You posted code which is not related to the log you posted. Also you said it failed to decode but there is no decode statement in your code.
I think, it's the other way around. He sends the json to a http server and gets the reply, it's not able to decode the json he sent.
 
Omega J Msigwa:
I have a JSON string that I'm trying to use to make a webrequest

When, Print to see how it looks like. I get 

I have tried to change json format and fix a line or two, but nothing seems to change

I could imagine time needs to be a long or ulong unix-timestamp, probably in UTC.
 
Dominik Christian Egert #: I think, it's the other way around. He sends the json to a http server and gets the reply, it's not able to decode the json he sent.

Whatever, the posted code and log are useless to help. The WebRequest code would be needed at least.

Why is there so much additional space at the bottom of your messages ?

 
Alain Verleyen #: Why is there so much additional space at the bottom of your messages ?

I've cleaned it up. I think it is because @Dominik Christian Egert usually types it out on a mobile phone.

 
Fernando Carreiro #:

I've cleaned it up. I think it is because @Dominik Christian Egert usually types it out on a mobile phone.

Thanks.
 
Fernando Carreiro #: I've cleaned it up. I think it is because @Dominik Christian Egert usually types it out on a mobile phone.

Thank you. Yes, I do use the mobile web page. Sometimes I have multiple CRs at the end...

But to be able to post at all, I first need to copy-paste a line with a CR in it, else my comment goes into the citation box.

 
Alain Verleyen #:

Whatever, the posted code and log are useless to help. The WebRequest code would be needed at least.

Why is there so much additional space at the bottom of your messages ?

Thanks for the response. I couldn't post the entire webrequest code because the issue seems to be with the way MQL5 interprets json object, the Webrequest had nothing to do with the error, the error it is in the json part, I was able to fix the issue by serializing the json object using this library https://www.mql5.com/en/code/13663 that  Fernando Carreiro recommended in other forum post. Here is how I did it

string headers = "Content-Type: application/json; charset=utf-8";
int timeout = 10000;
char res[];
string res_header;
string url = "http://127.0.0.1:5000/api/write";

  CJAVal jv;
 
   jv["time"]=(string)time; 
   jv["type"]=IntegerToString(type); 
   jv["volume"]=(string)volume;
   jv["stoploss"] = (string)stoploss;
   jv["takeprofit"] = (string)takeprofit;
   jv["price"] = string(price);
   
     
//--- serialize to string  

   char data[]; 
   ArrayResize(data, StringToCharArray(jv.Serialize(), data, 0, WHOLE_ARRAY)-1);
   
   //--- send data
   char res_data[];
   string res_headers=NULL;
   
   
   int ret = WebRequest("POST", url, headers, timeout, data, res_data, res_headers);
   
   if (ret == -1)
    {
      Print("Failed to Initiate WebRequest Err=",GetLastError());
      return false;
    }

   Print("Http Response code =",ret);   
JSON Serialization and Deserialization (native MQL)
JSON Serialization and Deserialization (native MQL)
  • www.mql5.com
Serialization and deserialization of JSON protocol. The code is ported from a high-speed С++ library.
 
Omega J Msigwa #:

Thanks for the response. I couldn't post the entire webrequest code because the issue seems to be with the way MQL5 interprets json object, the Webrequest had nothing to do with the error, the error it is in the json part, I was able to fix the issue by serializing the json object using this library https://www.mql5.com/en/code/13663 that  Fernando Carreiro recommended in other forum post. Here is how I did it

Of course it has to do with WebRequest() and how your data are provided, as you proved yourself. Not with "the way MQL5 interprets json object".

string json = "{\r\n"
                "\"time\": \"2023-06-06 10:00:00\",\r\n"
                "\"type\": 2,\r\n"
                "\"volume\": 0.02,\r\n"
                "\"stoploss\": 1.2000,\r\n"
                "\"takeprofit\": 1.2500,\r\n"
                "\"price\": 1.2200\r\n"
            "}";

Anyway, good you found a solution.

Reason: