Home > Software engineering >  Filtering JSON Response in C#
Filtering JSON Response in C#

Time:06-23

My Json data is like that.. I want to take monthlyAnnualCostRate by dueNum. So I want to say: What is the monthlyAnnualCostRate value of the data whose dueNum value is x?

For example; User giving info: dueNum=46 | Ok, I will calculate the monthlyAnnualCostRate by counting 1.8800

{
               "dueNum": 45,
               "installmentAmount": 28.89,
               "monthlyInterestRate": 1,
               "capitalAmount": 1000,
               "totalInstallmentAmount": 1300.03,
               "advancePaymentAmount": 0,
               "expenseAmount": 100,
               "annualCostRate": 22.7034,
               "monthlyContractRate": 1,
         "monthlyAnnualCostRate": 1.8920,
               "interestAmount": 250.01,
               "fundAmount": 37.52,
               "taxAmount": 12.5
            },
                  {
               "dueNum": 46,
               "installmentAmount": 28.42,
               "monthlyInterestRate": 1,
               "capitalAmount": 1000,
               "totalInstallmentAmount": 1307.03,
               "advancePaymentAmount": 0,
               "expenseAmount": 100,
               "annualCostRate": 22.5604,
               "monthlyContractRate": 1,
         "monthlyAnnualCostRate": 1.8800,
               "interestAmount": 255.83,
               "fundAmount": 38.39,
               "taxAmount": 12.81
            },
                  {
               "dueNum": 47,
               "installmentAmount": 27.96,
               "monthlyInterestRate": 1,
               "capitalAmount": 1000,
               "totalInstallmentAmount": 1314.22,
               "advancePaymentAmount": 0,
               "expenseAmount": 100,
               "annualCostRate": 22.4208,
               "monthlyContractRate": 1,
         "monthlyAnnualCostRate": 1.8684,
               "interestAmount": 261.85,
               "fundAmount": 39.27,
               "taxAmount": 13.1
            },

And It's my codes. I tried this process but i take error. Error: "Newtonsoft.Json.JsonReaderException: 'Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject. Path '', line 1, position 1.' "

My Codes:

string uri = "https://apis.garantibbva.com.tr:443/loans/v1/paymentPlan";
            var client = new RestClient(uri);
            var request = new RestRequest(Method.POST);
            request.AddHeader("apikey", "l7xx8af86c14ea7e44e0ab3fbfcc6137ae09");
            request.AddQueryParameter("loanType", "1");
            request.AddQueryParameter("campaignCode", "BankSearch");
            request.AddQueryParameter("loanAmount", "10000");
            IRestResponse response = client.Execute(request);
            
            var dueNum = 46;
            double monthlyAnnualCostRate = JArray.Parse(response.Content)
                .Where(p => (int)p["dueNum"]==dueNum)
                .Select(p => (double)p["monthlyAnnualCostRate"])
                .FirstOrDefault();

            _logger.LogInformation("Garanti Bankası Response: {monthlyAnnualCostRate}", monthlyAnnualCostRate);
            _logger.LogInformation("Garanti Bankası geçti");

JSON Starting:

{
         "data": {"list":    [
                  {
               "dueNum": 3,
               "installmentAmount": 341.37,
               "monthlyInterestRate": 1,
               "capitalAmount": 1000,
               "totalInstallmentAmount": 1024.11,
               "advancePaymentAmount": 0,
               "expenseAmount": 100,
               "annualCostRate": 118.9411,
               "monthlyContractRate": 1,
         "monthlyAnnualCostRate": 9.9118,
               "interestAmount": 20.08,
               "fundAmount": 3.02,
               "taxAmount": 1.01
            },
                  {
               "dueNum": 4,
               "installmentAmount": 257.54,
               "monthlyInterestRate": 1,
               "capitalAmount": 1000,
               "totalInstallmentAmount": 1030.18,
               "advancePaymentAmount": 0,
               "expenseAmount": 100,
               "annualCostRate": 92.9855,
               "monthlyContractRate": 1,
         "monthlyAnnualCostRate": 7.7488,
               "interestAmount": 25.15,
               "fundAmount": 3.77,
               "taxAmount": 1.26
            },

CodePudding user response:

Try this:

var dueNum = 46;

decimal monthlyAnnualCostRate = ((JArray) JObject.Parse(json)["data"]["list"])
       .Where(p => (int)p["dueNum"] == dueNum)
       .Select(p => (double)p["monthlyAnnualCostRate"])
       .FirstOrDefault(); //1.88

CodePudding user response:

You have a JSON object which contains a data property, which contains a list property` which contains the array. The array is not the root.

So you need this

double monthlyAnnualCostRate = JObject.Parse(json)["data"]["list"]
    .Where(p => (int)p["dueNum"] == dueNum)
    .Select(p => (double)p["monthlyAnnualCostRate"])
    .FirstOrDefault();

Creating a proper object model (using Visual Studio's "Paste JSON as Classes") and then deserializing directly to that would make things a lot easier.

  • Related