Home > Back-end >  How can i improve filter on JSON?
How can i improve filter on JSON?

Time:08-15

As a title, i'm trying to access some child by following solution from this thread C# JSON.Net parse and get list of all elements matching a value using LINQ .

Here is my code

            var result = await response.Content.ReadAsStringAsync();
        var jsonData = (JObject)JsonConvert.DeserializeObject(result);
        var doc = (JContainer)jsonData["symbols"];
        var results = doc.Descendants()
                         .OfType<JObject>()
                         .Where(x => x["filterType"] != null &&
                                     x["filterType"].Value<string>() == "MARKET_LOT_SIZE").ToList();

Result is pretty good so far, Here is sample result what i get from trial and error

                {
                "stepSize": "0.001",
                "filterType": "MARKET_LOT_SIZE",
                "maxQty": "120",
                "minQty": "0.001"
            },
            {
                "stepSize": "0.001",
                "filterType": "MARKET_LOT_SIZE",
                "maxQty": "140",
                "minQty": "0.002"
            }

It turn out that result is skip a lot of child to check, but that's not what i expected i still expect to check upper child to make sure i access child correctly.

Here is real response

{
"timezone": "UTC",
"serverTime": 1660492828507,
"futuresType": "U_MARGINED",
"rateLimits": [
    {
        "rateLimitType": "REQUEST_WEIGHT",
        "interval": "MINUTE",
        "intervalNum": 1,
        "limit": 2400
    },
    {
        "rateLimitType": "ORDERS",
        "interval": "MINUTE",
        "intervalNum": 1,
        "limit": 1200
    },
    {
        "rateLimitType": "ORDERS",
        "interval": "SECOND",
        "intervalNum": 10,
        "limit": 300
    }
],
"exchangeFilters": [],
"assets": [
    {
        "asset": "USDT",
        "marginAvailable": true,
        "autoAssetExchange": "-10000"
    },
    {
        "asset": "BTC",
        "marginAvailable": true,
        "autoAssetExchange": "-0.00100000"
    },
    {
        "asset": "BNB",
        "marginAvailable": true,
        "autoAssetExchange": "-10"
    },
    {
        "asset": "ETH",
        "marginAvailable": true,
        "autoAssetExchange": "-5"
    },
    {
        "asset": "XRP",
        "marginAvailable": true,
        "autoAssetExchange": "0"
    },
    {
        "asset": "ADA",
        "marginAvailable": true,
        "autoAssetExchange": "0"
    },
    {
        "asset": "USDC",
        "marginAvailable": true,
        "autoAssetExchange": "-10000"
    },
    {
        "asset": "DOT",
        "marginAvailable": true,
        "autoAssetExchange": "0"
    },
    {
        "asset": "SOL",
        "marginAvailable": true,
        "autoAssetExchange": "0"
    },
    {
        "asset": "BUSD",
        "marginAvailable": true,
        "autoAssetExchange": "-10000"
    }
],
"symbols": [
    {
        "symbol": "BTCUSDT",
        "pair": "BTCUSDT",
        "contractType": "PERPETUAL",
        "deliveryDate": 4133404800000,
        "onboardDate": 1569398400000,
        "status": "TRADING",
        "maintMarginPercent": "2.5000",
        "requiredMarginPercent": "5.0000",
        "baseAsset": "BTC",
        "quoteAsset": "USDT",
        "marginAsset": "USDT",
        "pricePrecision": 2,
        "quantityPrecision": 3,
        "baseAssetPrecision": 8,
        "quotePrecision": 8,
        "underlyingType": "COIN",
        "underlyingSubType": [
            "PoW"
        ],
        "settlePlan": 0,
        "triggerProtect": "0.0500",
        "liquidationFee": "0.015000",
        "marketTakeBound": "0.05",
        "filters": [
            {
                "minPrice": "556.80",
                "maxPrice": "4529764",
                "filterType": "PRICE_FILTER",
                "tickSize": "0.10"
            },
            {
                "stepSize": "0.001",
                "filterType": "LOT_SIZE",
                "maxQty": "1000",
                "minQty": "0.001"
            },
            {
                "stepSize": "0.001",
                "filterType": "MARKET_LOT_SIZE",
                "maxQty": "120",
                "minQty": "0.001"
            },
            {
                "limit": 200,
                "filterType": "MAX_NUM_ORDERS"
            },
            {
                "limit": 10,
                "filterType": "MAX_NUM_ALGO_ORDERS"
            },
            {
                "notional": "5",
                "filterType": "MIN_NOTIONAL"
            },
            {
                "multiplierDown": "0.9500",
                "multiplierUp": "1.0500",
                "multiplierDecimal": "4",
                "filterType": "PERCENT_PRICE"
            }
        ],
        "orderTypes": [
            "LIMIT",
            "MARKET",
            "STOP",
            "STOP_MARKET",
            "TAKE_PROFIT",
            "TAKE_PROFIT_MARKET",
            "TRAILING_STOP_MARKET"
        ],
        "timeInForce": [
            "GTC",
            "IOC",
            "FOK",
            "GTX"
        ]
    },
    {
        "symbol": "ETHUSDT",
        "pair": "ETHUSDT",
        "contractType": "PERPETUAL",
        "deliveryDate": 4133404800000,
        "onboardDate": 1569398400000,
        "status": "TRADING",
        "maintMarginPercent": "2.5000",
        "requiredMarginPercent": "5.0000",
        "baseAsset": "ETH",
        "quoteAsset": "USDT",
        "marginAsset": "USDT",
        "pricePrecision": 2,
        "quantityPrecision": 3,
        "baseAssetPrecision": 8,
        "quotePrecision": 8,
        "underlyingType": "COIN",
        "underlyingSubType": [
            "Layer-1"
        ],
        "settlePlan": 0,
        "triggerProtect": "0.0500",
        "liquidationFee": "0.015000",
        "marketTakeBound": "0.05",
        "filters": [
            {
                "minPrice": "39.86",
                "maxPrice": "306177",
                "filterType": "PRICE_FILTER",
                "tickSize": "0.01"
            },
            {
                "stepSize": "0.001",
                "filterType": "LOT_SIZE",
                "maxQty": "10000",
                "minQty": "0.001"
            },
            {
                "stepSize": "0.001",
                "filterType": "MARKET_LOT_SIZE",
                "maxQty": "2000",
                "minQty": "0.001"
            },
            {
                "limit": 200,
                "filterType": "MAX_NUM_ORDERS"
            },
            {
                "limit": 10,
                "filterType": "MAX_NUM_ALGO_ORDERS"
            },
            {
                "notional": "5",
                "filterType": "MIN_NOTIONAL"
            },
            {
                "multiplierDown": "0.9500",
                "multiplierUp": "1.0500",
                "multiplierDecimal": "4",
                "filterType": "PERCENT_PRICE"
            }
        ],
        "orderTypes": [
            "LIMIT",
            "MARKET",
            "STOP",
            "STOP_MARKET",
            "TAKE_PROFIT",
            "TAKE_PROFIT_MARKET",
            "TRAILING_STOP_MARKET"
        ],
        "timeInForce": [
            "GTC",
            "IOC",
            "FOK",
            "GTX"
        ]
    }
    ]

}

I want to get maxQty of MARKET_LOT_SIZE where symbol is BTCUSDT and contractType is PERPETUAL. What should i add more filter on this

CodePudding user response:

For your requirement I suggest to create a class and use LINQ on top of that. This will help you in adding conditions quickly

Step1: use json2csharp to convert the json to C# Model

Step2:DeserializeObject using Newtonsoft.JsonConvert

Step3: Add LINQ to get desired output

//STEP2
var doc = JsonConvert.DeserializeObject<Root>(jsonData);

//STEP3
var maxQty = doc.symbols.Where(s => s.symbol == "BTCUSDT" && s.contractType == "PERPETUAL")
                    .SelectMany(x => x.filters)
                    .Where(y => y.filterType == "MARKET_LOT_SIZE")
                    .Max(x => x.maxQty);
//STEP1
public class Asset
{
    public string asset { get; set; }
    public bool marginAvailable { get; set; }
    public string autoAssetExchange { get; set; }
}

public class Filter
{
    public string minPrice { get; set; }
    public string maxPrice { get; set; }
    public string filterType { get; set; }
    public string tickSize { get; set; }
    public string stepSize { get; set; }
    public string maxQty { get; set; }
    public string minQty { get; set; }
    public int? limit { get; set; }
    public string notional { get; set; }
    public string multiplierDown { get; set; }
    public string multiplierUp { get; set; }
    public string multiplierDecimal { get; set; }
}

public class RateLimit
{
    public string rateLimitType { get; set; }
    public string interval { get; set; }
    public int intervalNum { get; set; }
    public int limit { get; set; }
}

public class Root
{
    public string timezone { get; set; }
    public long serverTime { get; set; }
    public string futuresType { get; set; }
    public List<RateLimit> rateLimits { get; set; }
    public List<object> exchangeFilters { get; set; }
    public List<Asset> assets { get; set; }
    public List<Symbol> symbols { get; set; }
}

public class Symbol
{
    public string symbol { get; set; }
    public string pair { get; set; }
    public string contractType { get; set; }
    public object deliveryDate { get; set; }
    public object onboardDate { get; set; }
    public string status { get; set; }
    public string maintMarginPercent { get; set; }
    public string requiredMarginPercent { get; set; }
    public string baseAsset { get; set; }
    public string quoteAsset { get; set; }
    public string marginAsset { get; set; }
    public int pricePrecision { get; set; }
    public int quantityPrecision { get; set; }
    public int baseAssetPrecision { get; set; }
    public int quotePrecision { get; set; }
    public string underlyingType { get; set; }
    public List<string> underlyingSubType { get; set; }
    public int settlePlan { get; set; }
    public string triggerProtect { get; set; }
    public string liquidationFee { get; set; }
    public string marketTakeBound { get; set; }
    public List<Filter> filters { get; set; }
    public List<string> orderTypes { get; set; }
    public List<string> timeInForce { get; set; }
}
  • Related