22

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.

0

6 Answers 6

24

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
        );
}
Sign up to request clarification or add additional context in comments.

3 Comments

This to me, is a sign of code smell. Why is there a ReferenceLoop in this model? Is this a good case for ViewModel + AutoMapper?
I ran into this today and this fixed my issue. I'm shocked more people don't run into this. Thanks for the tip!
best answer!! It solves the problem, indeed!
4

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.

Comments

1

The truncation at 2033 makes me think that you're only grabbing the first piece of what SQL Server is returning. You need to concatenate all the results together, e.g.:

var queryWithForJson = "SELECT ... FOR JSON";
var conn = new SqlConnection("<connection string>");
var cmd = new SqlCommand(queryWithForJson, conn);
conn.Open();
var jsonResult = new StringBuilder();
var reader = cmd.ExecuteReader();
if (!reader.HasRows)
{
    jsonResult.Append("[]");
}
else
{
    while (reader.Read())
    {
        jsonResult.Append(reader.GetValue(0).ToString());
    }
}

Comments

0

For anyone who might end up here after seeing this when serving spatial types . . .

@IngoB 's answer made the issue go away, but then (along with the code-smell comment from @FailedUnitTest ringing true) there was a giant wad of stuff I wasn't expecting in the JSON for my simple Point. Setting ReferenceLoopHandling to Serialize and debugging allowed me to see details about what was happening, and eventually I ended up here.

Synopsis:

  1. Install the NetTopologySuite.IO.GeoJSON package
  2. Throw something like this in ConfigureServices():

    services.AddMvc(options =>
        {
            options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(Point)));
        })
        .AddJsonOptions(options =>
        {
            foreach (var converter in GeoJsonSerializer.Create(new GeometryFactory(new PrecisionModel(), 4326)).Converters)
            {
                options.SerializerSettings.Converters.Add(converter);
            }
        });
    

Comments

0

I know this is a bit late and might be answered already by others in other posts, but I fixed this by serializing with a reader the sql query and returning the object which gets transformed instantly into JSON without returning the query as JSON from the start.

Comments

0

My scenario was

  1. .NET Core 3.1,
  2. Newtonsoft JSON for serialization, and
  3. a custom middleware to change the response body (response wrapping).

The solution described at JSON response breaks in ASP.NET Core 3.1 Web API with custom response wrapper worked for me.

Also, https://github.com/dotnet/core/issues/1240 has some useful discussion.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.