Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json response gets truncated in web api using asp.net core

Tags:

I have a web api that returns a json object for me to use on my website. the problem is this:

[
{
    "installment": 1,
    "date": "03\/01\/2016",
    "amount": "27.28",
    "status": "\"01BI000657\""
},
{
    "installment": 2,
    "date": "04\/01\/2016",
    "amount": "49.25",
    "status": "\"01BI000699\""
},
{
    "installment": 3,
    "date": "05\/01\/2016",
    "amount": "56.31",
    "status": "\"01BI000745\""
},
{
    "installment": 4,
    "date": "06\/01\/2016",
    "amount": "53.43",
    "status": "\"01BI000811\""
},
{
    "installment": 5,
    "date": "07\/01\/2016",
    "amount": "60.52",
    "status": "\"01EI279932\""
},
{
    "installment": 6,
    "date": "08\/01\/2016",
    "amount": "57.95",
    "status": "\"01BI000934\""
},
{
    "installment": 7,
    "date": "09\/01\/2016",
    "amount": "60.24",
    "status": "\"01BI001015\""
},
{
    "installment": 8,
    "date": "10\/01\/2016",
    "amount": "67.36",
    "status": "\"01EI298127\""
},
{
    "installment": 9,
    "date": "11\/01\/2016",
    "amount": "65.30",
    "status": "\"01BI001185\""
},
{
    "installment": 10,
    "date": "12\/01\/2016",
    "amount": "72.44",
    "status": "\"01BI001277\""
},
{
    "installment": 11,
    "date": "01\/01\/2017",
    "amount": "70.75",
    "status": "\"01BI001380\""
},
{
    "installment": 12,
    "date": "02\/01\/2017",
    "amount": "73.55",
    "status": "\"01BI001486\""
},
{
    "installment": 13,
    "date": "03\/01\/2017",
    "amount": "89.28",
    "status": "\"01BI001567\""
},
{
    "installment": 14,
    "date": "04\/01\/2017",
    "amount": "80.00",
    "status": "\"01BI001691\""
},
{
    "installment": 15,
    "date": "05\/01\/2017",
    "amount": "87.23",
    "status": "\"01BI001822\""
},
{
    "installment": 16,
    "date": "06\/01\/2017",
    "amount": "86.63",
    "status": "\"01BI002011\""
},
{
    "installment": 17,
    "date": "07\/01\/2017",
    "amount": "93.89",
    "status": "\"01BI002172\""
},
{
    "installment": 18,
    "date": "08\/01\/2017",
    "amount": "93.78",
    "status": "\"01BI002369\""
},
{
    "installment": 19,
    "date": "09\/01\/2017",
    "amount": "97.49",
    "status": "\"\""
},
{
    "installment": 20,
    "date": "10\/01\/2017",
    "amount": "104.81",
    "status": "\"\""
},
{
    "installment": 21,
    "date": "11\/01\/2017",
    "amount": "105.50",
    "status": "\"\""
},
{
    "installment": 22,
    "date": "12\/01\/2017",
    "amount": "112.87",
    "status": "\"\""
},
{
    "installment": 23,
    "date": "01\/01\/2018",
    "amount": "114.15",
    "status": "\"\""
},
{
    "installment": 24,
    "date": "02\/01\/2018",
    "amount": "118.67",
    "status": "\"\""
},
{
    "installment": 25,
    "date": "03\/01\/2018",
    "amount": "131.57",
    "status": "\"\""
},
{"ins

as you can see it gets truncated, it weights 20kb, string length is 2033, so I wanted to know if there is a way to increase the max size of the response somehow. I tried the MaxJsonLength inside the web.config but it is not working, might be because of the .net core aspect so I am kinda lost, why it is getting truncated.

like image 448
V. Benavides Avatar asked Aug 12 '17 22:08

V. Benavides


2 Answers

I don't know why the json response gets truncated at some point, but in my case (ASP.NET Core 2.0) I had to tell Newtonsoft.Json to ignore reference loops like so:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()
        .AddJsonOptions(
            options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
        );
}
like image 110
IngoB Avatar answered Sep 21 '22 06:09

IngoB


One reason this can happen is if an error happens while serializing the object to JSON. For example a field marked as required that is not present in the data can cause this. The output simply stops and no exception is reported in this case.

Check that you can serialize the json object using JSonConvert.SerializeObject() before returning it and fix up any issues.

like image 23
Ian Mercer Avatar answered Sep 23 '22 06:09

Ian Mercer