how to write code that parse my json that I got with webrequest

 

how to write code that parse my json that I got with webrequest


I take json data from website using webrequest now How can I parse this json code for use my trading decisions 


//+------------------------------------------------------------------+
//|                                                  forex-robot.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"


//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
    string cookie=NULL,headers;
    char   post[],result[];
    string url = "https://webhook.site/..";

      // Make a GET request to the webhook.site URL
    int res=WebRequest("GET",url,cookie,NULL,500,post,0,result,headers);
    
    // Print the JSON data
    Print("Web request result: ", res, ", error: #", (res == -1 ? GetLastError() : 0));
    
    
  }
//+------------------------------------------------------------------+
MQL Parsing by Means of MQL
MQL Parsing by Means of MQL
  • www.mql5.com
The article describes a preprocessor, a scanner, and a parser to be used in parsing the MQL-based source codes. MQL implementation is attached.
 
Caner Efe Altıntaş:

how to write code that parse my json that I got with webrequest


I take json data from website using webrequest now How can I parse this json code for use my trading decisions 


There is a built in Json library by default i think in the includes folder .

i'm attaching a file (with some personal additions)

And here is a codebase link with an example :

https://www.mql5.com/en/code/13663

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.
Files:
 
//+------------------------------------------------------------------+
//|                                                  forex-robot.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include "json.mqh"

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
    string cookie=NULL,headers;
    char   post[],result[];
    string url = "https://webhook.site/85564fea-5205-4a28-b2b8-45aff056310c";

      // Make a GET request to the webhook.site URL
    int res=WebRequest("GET",url,cookie,NULL,500,post,0,result,headers);
    
    // Print the JSON data
    Print("Web request result: ", res, ", error: #", (res == -1 ? GetLastError() : 0));
   
    
  }
//+------------------------------------------------------------------+

shouldn't I use it like this when I write I got this error message


can't open "C:\Users\FS\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075\MQL5\Scripts\json.mqh" include file forex-robot.mq5 10 11


 

okay sir I put the code that you sent me jason_with_search.mqh how can I acces the json codes and parse which I got with webrequest 

I try to say which codes should I write 

 
Caner Efe Altıntaş #:

okay sir I put the code that you sent me jason_with_search.mqh how can I acces the json codes and parse which I got with webrequest 

I try to say which codes should I write 

Do you have an example response the server sends ? (open the test.txt after running this) 

void OnStart()
  {
//---
    string cookie=NULL,headers;
    char   post[],result[];
    string url = "https://webhook.site/85564fea-5205-4a28-b2b8-45aff056310c";

      // Make a GET request to the webhook.site URL
    int res=WebRequest("GET",url,cookie,NULL,500,post,0,result,headers);
    
    // Print the JSON data
    Print("Web request result: ", res, ", error: #", (res == -1 ? GetLastError() : 0));
   string test_alert=CharArrayToString(result,0,ArraySize(result),CP_UTF8);
   CJAVal loader;
   loader.Deserialize(test_alert,CP_UTF8);
   //you then go into the data like accessing arrays , but first for a test you output what the result is 
   bool am_i_testing=true;
   if(am_i_testing){
int f=FileOpen("Test.txt",FILE_WRITE|FILE_TXT);
FileWriteString(f,test_alert);
FileClose(f);     
}
  }
 

Sir I runned the code that you sent me but I can't find the path of the Test.txt where is text created.

Maybe text file even dont create. 

 
Caner Efe Altıntaş #:

Sir I runned the code that you sent me but I can't find the path of the Test.txt where is text created.

Maybe text file even dont create. 

Try clicking on File >> OpenDataFolder >> MQL5 >> Files >> Test.txt

 
Okay I founded text file but there is nothing in the file .
 

What is the web request result that is printed in the Experts tab on the bottom ? 

does it say 503?

 


I got http 200 status code means successful meanwhile we are trying to get data, how can we get json data from webhook.site?

I think I have a connection with site right now even I can send post requests there but problem I cant take the json data this is my only question.

Thank you,

 
Caner Efe Altıntaş #:


I got http 200 status code means successful meanwhile we are trying to get data, how can we get json data from webhook.site?

I think I have a connection with site right now even I can send post requests there but problem I cant take the json data this is my only question.

Thank you,

Are you sure you are not getting an empty response ? 

Place an :

Alert(test_alert);

after 

string test_alert=CharArrayToString(result,0,ArraySize(result),CP_UTF8);