A powerful and feature-rich MQL5 JSON library designed to bring a modern development experience similar to Python/JS - MetaTrader 5 Library
MQL5 JSON Library is a powerful and feature-rich library designed for parsing, manipulating and serializing JSON data in the MQL5 environment. It provides a simple and intuitive Document Object Model (DOM) API designed to make the JSON processing experience in MQL5 comparable to modern programming languages such as JavaScript and Python.
The library is capable of handling a wide range of tasks, from reading simple EA configurations to complex real-time data exchange between systems. Its main functions and features include:
Flexible parser : Optionally supports some non-standard features of JSON5, such as code comments and trailing commas, to enhance compatibility with a variety of data sources.
---Manipulation and Access---
Intuitive DOM traversal : Access data using the intuitive syntax of keys (node["key"]) and indexes (node[0]), just like using Python dictionaries or JavaScript objects.
Dynamic modification : Freely add, update or delete key-value pairs in JSON objects and elements in arrays (Set, Add, Remove).
---Advanced query and processing---
Powerful query engine : Built-in support for JSON pointers (RFC 6901, for direct path access) and JSON paths (for complex and fuzzy queries), capable of efficiently extracting one or more data nodes in batches or precisely from deeply nested complex structures.
Practical functions : Provides advanced functions such as document cloning (.Clone()) and deep merging (JsonMerge), which greatly simplifies common complex tasks, such as merging "default configuration" with "user configuration".
--- Robustness and Security ---
Automatic memory management : adopts RAII (resource acquisition is initialization) design pattern. JsonDocument is responsible for managing the life cycle of all its nodes. Developers do not need to manually add/delete any JSON elements, fundamentally eliminating the risk of memory leaks.
2. Core concepts and memory management
[!!] 2.1 Namespace - Key to Integration Projects [!!] The most important tip : all classes (e.g. JsonDocument, JsonNode) and global functions (e.g. JsonParse) in this library are encapsulated in a namespace called MQL5_Json.
**How to use it correctly:** - **In the header file (.mqh)**: MQL5 does not allow ` using namespaces` in the global scope of the header file . **Therefore, you must use the fully qualified name**. This is the only reliable method in multi- file projects. Incorrect: `JsonDocument doc;` Correct: `MQL5_Json::JsonDocument doc;` - **Internal functions in the main program file (.mq5)**: For convenience, you can use the ` Use namespace inside the function MQL5_Json;`, but to ensure this overview of this example, all code in this manual will use this fully qualified name method. **If you encounter this ` 'JsonNode' - Declaration without input` compilation error, ** ** it's almost always because you forgot to add the `MQL5_Json::` prefix ** ** to this type and function. **
[!!]2.2 Memory Management Model JsonDocument owns the data; JsonNode is just a view .
[!!] 2.3 Object passing in MQL5 MQL5 requires that all class objects (including JsonNode) passed as function parameters must be passed by reference (using &) . Error: void myFunction(MQL5_Json::JsonNode node) Correct: void myFunction(MQL5_Json::JsonNode &node)
Attachment download
📎 jsoncore.mqh (2.18 KB)
📎 jsonnode.mqh (5.67 KB)
📎 jsonparser.mqh (3.27 KB)
📎 jsonserializer.mqh (9.77 KB)
📎 jsonstream.mqh (4.82 KB)
📎 jsonstreamparser.mqh (3.05 KB)
📎 jsontypes.mqh (16.29 KB)
📎 jsonutils.mqh (3.87 KB)
📎 jsondocument.mqh (6.93 KB)
📎 jsonapiimpl.mqh (33.47 KB)
📎 jsonpath.mqh (23.42 KB)
📎test_jsonlib_script.mq5 (12.91 KB)
📎 jsonlib.mqh (34.62 KB)
Source: MQL5 #62735
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •