← Back to home
MT5 BINANCE / API RESPONSE

Binance API Responses | JSON Deserialization Guide

Handling JSON Responses from Binance APIs

Binance API endpoints return JSON data. Understanding JSON parsing is a core skill for Binance programming in MQL.

USDⓈ-M Futures Order Response Example

After an EA submits an order through the Binance API, Binance pushes a Trade Lite event.

Event documentation: https://developers.binance.com/en/docs/catalog/core-trading-derivatives-trading-usd-s-m-futures/api/ws-streams/~schemas#tradelite

An example of the event JSON is shown below:

      {
        "e":"TRADE_LITE",             // Event type
        "E":1721895408092,            // Event time
        "T":1721895408214,            // Transaction time
        "s":"BTCUSDT",                // Symbol
        "q":"0.001",                  // Original quantity
        "p":"0",                      // Original price
        "m":false,                    // Is this trade on the maker side?
        "c":"z8hcUoOsqEdKMeKPSABslD", // Client order ID
            // Special client order IDs:
            // Strings starting with "autoclose-": liquidation orders
            // "adl_autoclose": ADL auto-deleveraging orders
            // "settlement_autoclose-": Settlement orders for delisting or delivery
        "S":"BUY",                    // Side
        "L":"64089.20",               // Last filled price
        "l":"0.040",                  // Last filled quantity
        "t":109100866,                // Trade ID
        "i":8886774,                  // Order ID
      }

The EA uses the open-source JSON library from mql5.com to deserialize the JSON data returned by Binance https://www.mql5.com/en/code/13663.
After deserialization, retrieve the corresponding event data using the JSON keys, then store it in MQL variables for the EA to use.

  CJAVal jEvent;

      //Events pushed by Binance are in JSON format
      //Use JAson.mqh to deserialize the event JSON
      //deserialize binance response JSON  Deserialize the JSON returned by Binance
      jEvent.Deserialize(event);

      //Get the event type
      string e=jEvent["e"].ToStr();

      //calculate local timezone
      //Calculate the local computer time zone
      int timeZone=(int)(TimeLocal()-TimeGMT())/60/60;

  //Check the value of key e (event type) for a Trade Lite event
      if(e=="TRADE_LITE")
        {
         long eventTime=jEvent["E"].ToInt();    //Event Time Event time
         printf("TRADE_LITE|Event Time Event time %s", (string)eventTime);

         //Transaction Time
         //Key T is the transaction time, an integer timestamp; deserialize it into an integer variable
         long transactionTime=jEvent["T"].ToInt();
         printf("          |Transaction Time Transaction time %s", (string)transactionTime);

         //symbol
         //Key s is the symbol
         string eventSymbol=jEvent["s"].ToStr();
         printf("          |symbol Symbol %s", eventSymbol);

         //Original Quantity
         //Key q is the original order quantity
         string originalQuantity=jEvent["q"].ToStr();
         printf("          |Original Quantity Original quantity %s", originalQuantity);

         //Original Price
         //Key p is the original order price
         string originalPrice=jEvent["p"].ToStr();
         printf("          |Original Price Original price %s", originalPrice);

         //Is this trade the maker side?
         //Key m indicates whether this trade is on the maker side; deserialize the boolean into a bool variable
         bool isMaker=jEvent["m"].ToBool();
         printf("          |Is this trade the maker side? Is this trade on the maker side? %s", (string)isMaker);

         //Client Order Id
         //Key c is the client order ID
         string clientOrderId=jEvent["c"].ToStr();
         printf("          |Client Order Id Client order ID %s", clientOrderId);

         //Side
         //Key S is the order side
         string side=jEvent["S"].ToStr();
         printf("          |Side Side %s", side);

         //Last Filled Price
         //Key L is the last filled price
         string lastFilledPrice=jEvent["L"].ToStr();
         printf("          |Last Filled Price Last filled price %s", lastFilledPrice);

         //Order Last Filled Quantity
         //Key l is the last filled quantity
         string orderLastFilledQuantity=jEvent["l"].ToStr();
         printf("          |Order Last Filled Quantity Last filled quantity %s", orderLastFilledQuantity);

         //Trade Id
         //Key t is the trade ID, an integer; deserialize it into a long variable
         long tradeId=jEvent["t"].ToInt();
         printf("          |Trade Id Trade ID %d", tradeId);

         //Order Id
         //Key i is the order ID, an integer; deserialize it into a long variable
         long orderId=jEvent["i"].ToInt();
         printf("          |Order Id Order ID %d", orderId);
        }