JSON Parser
Neither MQL4 nor MQL5 has templates or a tagged-union type, which is normally where a from-scratch JSON parser in either language gets abandoned - there is nowhere obvious to put "a value that could be a string, a number, a nested object or an array of any of those". This header solves it by storing the whole parsed document as one flat array of SJsonNode structs, each node carrying a parent index. Tree navigation becomes a linear scan over a flat array instead of pointer chasing, and the parser never calls new - the document is one resizeable array, freed automatically when the CJsonParser instance goes out of scope. Values are read back by PATH, not by walking nodes by hand. GetString(path), GetNumber(path), GetBool(path) and friends resolve a path like "riskProfile.perTrade" or "legs[1].symbol" fresh on every call, each segment either a key lookup on an object or a bracketed position on an array. That keeps the public surface to a handful of GetX(path) calls with a default value, instead of a node-handle API the caller has to manage - and every GetX call takes a default, so a missing or wrong-typed path degrades to a sensible fallback rather than a runtime error. String literals are decoded properly, including \" \\ \/ \b \f \n \r \t and \uXXXX unicode escapes - the demo config includes a name with an escaped accented character to prove it. Numbers accept the full JSON grammar: optional sign, integer part, optional fraction, optional e/E exponent - the demo parses 1.5e2 and confirms it reads back as 150. A failed Parse() leaves any previously parsed document completely intact. The parser builds into a temporary node array and only replaces the live one on success, which matters for a program that re-reads a config file periodically: a config edited mid-save into invalid JSON must not wipe out the last good settings the EA is currently running on. The demo proves this by parsing three broken documents in a row after a successful parse and then reading a value back to confirm nothing changed. Notes and limitations - Read-only. There is no serializer here - if you need to write JSON back out, build the string yourself; this header is for reading configs and API responses, which is the overwhelming majority of real JSON use in either language. - Numbers are always double. JSON does not distinguish integers from floats, and neither does this header - round or cast at the call site if you need an int. - GetKeys() and array indexing rely on document order as parsed, which this header preserves faithfully; it is not a sorted or hashed view. - Path resolution is a fresh scan from the root on every call. That is the right trade for typical config-sized documents (tens to low hundreds of nodes read a handful of times); a document parsed once and then queried in a tight per-tick loop should cache the values it needs rather than calling GetX() from inside OnTick() every time. - Not thread-safe, which matches both platforms' single-threaded-per-chart execution model; nothing here changes that.
Script · MT5 · v1.0 · Free
About JSON Parser
JSON Parser is a script for MetaTrader 5. It has 1 setting, such as Config label. MQL source code included.
Files in the download: JsonParser.mq5.mqh, JsonParser.txt, JsonParserDemo.ex5, JsonParserDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Config label | demo config | Label printed above the parsed document |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code