Libraries: JSON Serialization and Deserialization (native MQL) - page 17

[Deleted]  

This library causes ERROR and i confirmed it

ERR_WRONG_STRING_PARAMETER
 
5040
 
Damaged parameter of string type
 

my code

CJAVal  jv;
string text="abc";

jv.Deserialize(text);

Any idea how to fix it?

It only causes when i desearilize any string

 

If the string value does contain ';' character then Deserialize fails!

array out of range in 'JAson.mqh' (205,13)

 
Dmitry Zhakov #:

If the string value does contain ';' character then Deserialize fails!


Everything works. I found issue on my side.

 
Than you . I am sending POST WebRequests (http) from MT5 (MQL5) to Chat GPT-4-o-mini API (openai.com) . OpenAI receives JSON data . MQL does not support JSON . So had to covert to string, then convert string to Char[] array, to insert as postData into the WebRequest . I am using your Library, I'll let you know how I get on
 
Philip Kym Sang Nelson #:
Than you . I am sending POST WebRequests (http) from MT5 (MQL5) to Chat GPT-4-o-mini API (openai.com) . OpenAI receives JSON data . MQL does not support JSON . So had to covert to string, then convert string to Char[] array, to insert as postData into the WebRequest . I am using your Library, I'll let you know how I get on

That is awesome
I was thinking the same to feed my ML with response from ChatGPT.
Mind sharing how do you did it? What are the challenges?

Thanks!

 
It works perfectly Kudos :) Y'll go and check my post on how to use CJVAL class to parse json data  Extracting Specific Data from JSON Responses in MetaTrader 5
Trading blogs and financial markets analysis
Trading blogs and financial markets analysis
  • www.mql5.com
Read blogs to find the latest news on various topics from all over the world — rumors about companies, country and industry reports, market analysis, latest developments in speculative trading and more. Start your own blog to share new ideas and trading achievements with the members of MQL5.community. Post interesting images and videos, enjoy unlimited possibilities!
 

Hi, is there any way to:

- Set the value of a key to null 

- Set the value of a key to {} (empty object) 

It seems that those values won't get correctly represented if you try to set them.

Thanks for any reply

 

What am I doing wrong?
A real example from practice. A string 21+ thousand characters long. Several variables and 2 large arrays b and a. There are 500 subarrays of 2 elements in each:

"b":[["0.0018659","8500"],["0.0018655","16800"],...],
"a":[["0.0018659","8500"],["0.0018655","16800"],...],


I found a discrepancy between the original data and what JASON outputs:

["0.0019640","800"],["0.0019641","91500"],["0.0019644","96600"],["0.0019645","503900"],["0.0019646","101600"],
["0.0019649","500"],["0.0019650","300"],["0.0019651","1200"],["0.0019652","500"],["0.0019653","800"]],
n=2000
[ 0]      0.0018659,  8500.0000000,     0.0018655, 16800.0000000,     0.0018654,153000.0000000,     0.0018653,  3800.0000000,     0.0018650,   300.0000000
[10]      0.0018649,   900.0000000,     0.0018647,  2900.0000000,     0.0018645,  6700.0000000,     0.0018644, 33700.0000000,     0.0018640,  9600.0000000
[ 0]     0.0019640,  200.0000000,    0.0019641,25600.0000000,    0.0019644, 3000.0000000,    0.0019645, 3800.0000000,    0.0019646,  200.0000000
[10]     0.0019649, 3600.0000000,    0.0019650,  700.0000000,    0.0019651,  200.0000000,    0.0019652,  700.0000000,    0.0019653,  100.0000000

the first 2 lines output the end of the original string, which is parsed by JASON.
When parsing, all elements are written to the array. It gets n=2000 elements. This is correct.
Then 2 lines are the first 20 elements of the array and 2 more lines of the last 20 elements.
At the beginning of the array everything coincides.
But at the end the first digit of the pairs coincides, but the second one does not. Apparently it was taken from somewhere else.

I checked. The second array a contains data from the first array b. Here's the end of the original data from array a:

,["0.0017785","200"],["0.0017784","3600"],["0.0017783","700"],["0.0017782","200"],["0.0017781","700"],["0.0017780","100"]],

I.e. the second array in subarrays in the first elements contains its /right value, in the second elements - values from the first array.
The problem is probably in the parser code.

Data substitution is very stressful - how to test on them later? Only incorrectly! Lucky you noticed it. Hopefully for simpler variants without nested arrays it works correctly. But you should test everything before putting it to work.

The code is based on the examples from the branch, only with my data string:

 void TestJAson(string msg) {
   CJAVal json; 
   json.Deserialize(msg);
   double bin[];int n=0;
   int s=ArraySize(json["data"]["b"].m_e);
   ArrayResize(bin, n+s*2, 100000);
   for(int  i=0; i<s; i++){
      bin[n]=json["data"]["b"][i][0].ToDbl(); n++;//price
      bin[n]=json["data"]["b"][i][1].ToDbl();; n++;//volume
   }

   s=ArraySize(json["data"]["a"].m_e);
   ArrayResize(bin, n+s*2, 100000);
   for(int  i=0; i<s; i++){
      bin[n]=json["data"]["a"][i][0].ToDbl(); n++;//price
      bin[n]=json["data"]["b"][i][1].ToDbl(); n++;//volume
   }
   
   Print(StringSubstr(msg,StringLen(msg)-300,300));
   
   int tot=ArraySize(bin);
   Print("n=",tot);
   ArrayPrint(bin,7,",",0,20);
   ArrayPrint(bin,7,",",tot-20,20);
}

void OnStart(){
   ulong from=GetMicrosecondCount();
   TestJAson(message);
   ulong to=GetMicrosecondCount();
   PrintFormat("MQL %lu mcs/iteration",(to-from));
}

The full code of the script with this string is attached.
Library of the latest version "1.13" - downloaded from GitHub.

Files:
3.mq5  53 kb
 

Above also write about problems with the second element. Probably the same problem.

I made myself a data parsing for a particular template, through the split by quotes " with the subsequent selection of elements by number.

Minus - for each template will have to spend time and write their own parser and count the numbers of elements ...
Plus - 3 times faster universal and convenient JASON. Instead of 40 seconds 13 on the initial data ~400 Mb.

Spent half a day on it.
More precisely on 3 versions:
1) first I searched for specific substrings in char array and then assembled strings from their values - I assumed that this would be the fastest code
I wanted to work directly with char array, because I have the original one from WebRequest or FileOpen and didn't want to assemble it into a large string for speed.
2) then from char[] I created strings only with arrays b and a - and split
3) and finally the simplest code - each string was collected into one big text string (with all data including unnecessary ones) and split it

It turned out that all 3 variants are about the same in speed. That's why I decided on the simplest - the 3rd variant. It will be easier to modify it later to other templates with other data.

For some reason I thought that working with char array will be faster than assembling it into a string and splitting it into a string array via ". Apparently split is well optimised for speed.

It would be good if MQ developers added some of the standard JSON parsers to the language. JASON does not parse all variants correctly and apparently the developer is no longer here and there is no hope for a fix.
 
Forester #:
Library of the latest version "1.13" - downloaded from GitHub

There is a rewritten fork of the code from this topik (1.12). Have you checked with the original?

PS: I have 1.12 original and heavily corrected. I don't remember where the corrections came from, probably from this branch.