Can anyone help me to use a JSON parser library for MQL5

 

Hi coders,

I am looking for a JSON parser library for my MQL5 project. I have read some data online through WebRequest which is JSON data. I need to read it from MQL5 code to get the value.

Thanks in advanced.

#JSON_Parser

#MQL5_Code

 
Al Imran Suvro:

Hi coders,

I am looking for a JSON parser library for my MQL5 project. I have read some data online through WebRequest which is JSON data. I need to read it from MQL5 code to get the value.

Thanks in advanced.

#JSON_Parser

#MQL5_Code

Something like this? https://www.mql5.com/en/code/13663

JSON Serialization and Deserialization (native MQL)
JSON Serialization and Deserialization (native MQL)
  • www.mql5.com
ForecastOscilator_HTF The ForecastOscilator indicator with the timeframe selection option available in the input parameters. Flat_HTF The Flat indicator with the timeframe selection option available in the input parameters. FX5_SelfAdjustingRSI_HTF The FX5_SelfAdjustingRSI...
 
Fernando Morales:

Something like this? https://www.mql5.com/en/code/13663

Thank you so much 'Fernando Morales'. It's work well.

 

please how can i adapt the json library parser to deserialize and loop through this list

[
  {
    "time": "2025-02-05T16:17:00",
    "support1": 1.25069,
    "resistance1": 1.2511999999999999,
    "support2": 0.0,
    "resistance2": 0.0,
    "pivot_low": 1.25069,
    "pivot_high": 1.25089,
    "effective_pivot": 1.25069,
    "Trend": true,
    "Tick_command": "Buy_now",
    "id": 648
  },
  {
    "time": "2025-02-05T16:06:00",
    "support1": 1.25149,
    "resistance1": 1.25173,
    "support2": 0.0,
    "resistance2": 0.0,
    "pivot_low": 1.2516,
    "pivot_high": 1.25203,
    "effective_pivot": 1.25203,
    "Trend": true,
    "Tick_command": "Sell_now",
    "id": 647
  },
  {
    "time": "2025-02-05T15:56:00",
    "support1": 1.25119,
    "resistance1": 1.25164,
    "support2": 0.0,
    "resistance2": 0.0,
    "pivot_low": 1.25119,
    "pivot_high": 1.25157,
    "effective_pivot": 1.25119,
    "Trend": true,
    "Tick_command": "Buy_now",
    "id": 646
  }
]

Improperly formatted data edited by moderator. Please use the CODE button (Alt-S) when inserting code or data and format/style it properly.

Code button in editor

 

in my experience working with the JAson library is that, It works best when a json result is in a simple format (wrapped in a single curly bracket), with no arrays and fancy objects just scalars.

In your case you might want to return a simple json format at once.

  {
    "time": "2025-02-05T16:17:00",
    "support1": 1.25069,
    "resistance1": 1.2511999999999999,
    "support2": 0.0,
    "resistance2": 0.0,
    "pivot_low": 1.25069,
    "pivot_high": 1.25089,
    "effective_pivot": 1.25069,
    "Trend": true,
    "Tick_command": "Buy_now",
    "id": 648
  }
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.
 
Forexsunnet #:

Hi ,this applies to the data structure you shared , if the data structure changes you will have to re-adapt the code.

#include "jason_with_search.mqh";
int OnInit()
  {
  EventSetMillisecondTimer(44); 
  return(INIT_SUCCEEDED);
  }
void OnTimer(){
  EventKillTimer();
  //""receive"" the string
    if(FileIsExist("feed.txt")){
    int f=FileOpen("feed.txt",FILE_READ|FILE_TXT|FILE_ANSI);
    if(f!=INVALID_HANDLE){
    string feed="";
    while(!FileIsEnding(f)){
         feed+=FileReadString(f);
         }
    FileClose(f);
    CJAVal loader;
    loader.Clear();
    loader.Deserialize(feed,CP_ACP);
    int total_items=ArraySize(loader.m_e);
    Print("loader items "+IntegerToString(total_items));
    //loop to the items 
      for(int i=0;i<total_items;i++){
         //get time
           Print("Item("+IntegerToString(i)+") Time("+loader[i]["time"].ToStr()+")");
         }
    }else{Print("Cannot open file");}
    }else{Print("Test file does not exist");}
  }
void OnDeinit(const int reason)
  {
  }
void OnTick()
  {
  }
Files:
 
Forexsunnet #:

please how can i adapt the json library parser to deserialize and loop through this list

What do you mean? A proper JSON parser should parse this without adaptation and return an array of objects.

 
Lorentzos Roussos #:

Hi ,this applies to the data structure you shared , if the data structure changes you will have to re-adapt the code.

Thanks man. You saved my day. God bless you man