190

I am using jquery, JSON, and AJAX for a comment system. I am curious, is there a size limit on what you can send through/store with JSON? Like if a user types a large amount and I send it through JSON is there some sort of maximum limit?

Also can any kind of text be sent through JSON. for example sometime I allow users to use html, will this be ok?

scunliffe
  • 62,582
  • 25
  • 126
  • 161
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
  • Actually, i think there's some kind of limit but i have not been able to determine it; here's the case: I have an Asp.Net page (don't judge me, i was asked to work with it) which queries the DB and gets a datatable with around 2000 rows and 27 columns. If i drop the datatable to an XML, the request from the client works, slow but works, since js is creating the table and doing some stuff.. anyway... I did a DataTable To jSon routine which has always worked for me like a charm, but in this case, that huge amount of data seems to be truncated, or something else is going on, but the server respon – Sam Ccp Aug 09 '11 at 16:26
  • 9
    Be careful how you interpret people's answers below! Most answers say there are no limits to 'JSON' itself. While most servers will have a configurable limit. I ran a simple AJAX test where I incremented a byte on each send and at approx 8K bytes, it failed on several PHP/Apache servers I tried now. Error was: "414 (Request-URI Too Large)" – Jeach Apr 29 '13 at 22:00
  • 4
    We reliably send/receive 100kb payloads on iOS/iPhone. Something to beware of is that many protocols receive data in chunks, and attempting to deserialize the chunk rather than waiting for all the data to be received will result in failure, unless your deserializer logic is specially set up for it. – Hot Licks Nov 21 '13 at 12:16
  • 1
    @Jeach - That failure was almost certainly due to the data transmission being broken into 8K blocks, then not properly reassembled on the other end before attempting to parse. – Hot Licks Jul 16 '14 at 16:18
  • @HotLicks I'm not sure what the error was or what caused it, but I do know that my tests were done on reliable 1GB/s dedicated links. So if it failed there, it would definitely fail for your common browser/server links. The product I tested it with is a commercial product we sell that reliably transfers multiple GB of audio per second. I will look into what you mean by broken 8K blocks but in the end I would expect it to fail as much (or more) when used in the cloud. – Jeach Jul 17 '14 at 05:47
  • 1
    @Jeach - This is a common coding error. Waiting only for the first response message from a request vs waiting for the last and concatenating the response data together. Each response will contain at most 8K, so for longer data multiple responses are used. – Hot Licks Jul 17 '14 at 12:17
  • 1
    @Jeach - If you're doing this in iOS, with a standard NSURLConnectionh, you need to implement `connectionDidFinishLoading` and do the JSON translation there (or after that is called), not in `didReceiveData` (where you should simply concatenate the received data with the date previously received). Other environments are similar. – Hot Licks Jul 17 '14 at 12:26
  • I'm getting a `IndexSizeError: Index or size is negative or greater than the allowed amount.` but I see it's happening when trying to output an array after the data is already received. – TARKUS Jun 15 '16 at 17:17

9 Answers9

169

JSON is similar to other data formats like XML - if you need to transmit more data, you just send more data. There's no inherent size limitation to the JSON request. Any limitation would be set by the server parsing the request. (For instance, ASP.NET has the "MaxJsonLength" property of the serializer.)

Andrew P.
  • 161
  • 9
Amber
  • 507,862
  • 82
  • 626
  • 550
  • 1
    so technicly, If I wanted to take the WHOLE sourcecode to this whole page, it could be sent as a json object? – JasonDavis Aug 11 '09 at 19:27
  • This page's code is actually only 6.7kb (not counting external resources). That'd easily be done with just about any type of HTTP request. – Amber Aug 11 '09 at 19:31
  • 20
    It may be worth noting that the "default is 2097152 characters, which is equivalent to 4 MB of Unicode string data" for the property `JavaScriptSerializer.MaxJsonLength` mentioned in Amber's answer. (N.B. I have quoted from [MSDN](http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.maxjsonlength.aspx)) – dumbledad Dec 12 '12 at 20:38
  • So how would I parse a 155kb JSON object? – Ray Jul 23 '13 at 21:31
  • The server does not parse I believe, the client does, the server serializes. Is there a limit on traditional javascript interpreters like v8, nitro, and spidermonkey? – Asad Hasan Nov 13 '13 at 18:05
  • 2
    @AsadHasan A server can parse or serialize, depending on which direction the data is going. – Amber Nov 13 '13 at 18:06
  • what is more preferable keep limit JSON data or no limit, unlimited JSON data? – Mansukh Ahir Apr 08 '15 at 12:51
  • @Amber Dear i am getting data from sharepoint but its giving just 100 data where there is 700 data in json array.so i have to write loop for just 100 static else its give error array indexoutofbound – Vishal Thakkar Feb 17 '16 at 05:29
  • I'm getting a `IndexSizeError: Index or size is negative or greater than the allowed amount.` but I see it's happening when trying to output an array after the data is already received. – TARKUS Jun 15 '16 at 17:17
  • I've been doing my work setting a `MaxJsonLength` property for a while and I recently found out that firefox the response from the request will not be shown completely although there isn't any issue, it works perfectly fine. – Pankaja Gamage Jun 02 '17 at 07:03
26

There is no fixed limit on how large a JSON data block is or any of the fields.

There are limits to how much JSON the JavaScript implementation of various browsers can handle (e.g. around 40MB in my experience). See this question for example.

Community
  • 1
  • 1
cdiggins
  • 17,602
  • 7
  • 105
  • 102
  • 6
    8K is an exceptionally small limit for JSON. I suspect that's not a limit for the JSON, but rather the data is being transmitted in 8K blocks, and the code receiving the data is attempting to deserialize an individual block rather than waiting for the entire payload to arrive. – Hot Licks Nov 22 '13 at 01:57
  • I'd bet you are correct Hot Licks. I updated my answer. – cdiggins Jul 16 '14 at 16:07
10

Surely everyone's missed a trick here. The current file size limit of a json file is 18,446,744,073,709,551,616 characters or if you prefer bytes, or even 2^64 bytes if you're looking at 64 bit infrastructures at least.

For all intents, and purposes we can assume it's unlimited as you'll probably have a hard time hitting this issue...

Perfect64
  • 159
  • 1
  • 6
  • I agree, I've sent payloads in excess of 20GB with no issues, the limiting factor being the system on either end being able to handle that size of data, usually overridden in a `web.config` or somewhere like that (for C# Net.Core/7 etc) – djack109 Aug 30 '23 at 15:10
9

It depends on the implementation of your JSON writer/parser. Microsoft's DataContractJsonSerializer seems to have a hard limit around 8kb (8192 I think), and it will error out for larger strings.

Edit: We were able to resolve the 8K limit for JSON strings by setting the MaxJsonLength property in the web config as described in this answer: https://stackoverflow.com/a/1151993/61569

Community
  • 1
  • 1
Anthony F
  • 6,096
  • 4
  • 31
  • 32
  • 5
    An 8K "limit" is almost certainly a data transmission block boundary, not the limit for a JSON document. Larger strings can be processed by correctly reassembling the multi-block transmission. – Hot Licks Jul 16 '14 at 16:16
  • It's been a while since we figured out how to change the 8K limit in the DataContractJsonSerializer, but I think it was the "MaxJsonLength" setting (http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.maxjsonlength(v=vs.110).aspx) – Anthony F Jul 18 '14 at 18:49
8

Implementations are free to set limits on JSON documents, including the size, so choose your parser wisely. See RFC 7159, Section 9. Parsers:

"An implementation may set limits on the size of texts that it accepts. An implementation may set limits on the maximum depth of nesting. An implementation may set limits on the range and precision of numbers. An implementation may set limits on the length and character contents of strings."

Community
  • 1
  • 1
stleary
  • 422
  • 5
  • 9
6

There is really no limit on the size of JSON data to be send or receive. We can send Json data in file too. According to the capabilities of browser that you are working with, Json data can be handled.

5

If you are working with ASP.NET MVC, you can solve the problem by adding the MaxJsonLength to your result:

var jsonResult = Json(new
{
    draw = param.Draw,
    recordsTotal = count,
    recordsFiltered = count,
    data = result
}, JsonRequestBehavior.AllowGet);
jsonResult.MaxJsonLength = int.MaxValue;
mathias.horn
  • 241
  • 1
  • 8
  • 12
  • But this makes the max roughly 4MB, and we need it higher. How could we handle that scenario? – Casey ScriptFu Pharr Aug 26 '19 at 14:11
  • @Casey - what symptom happens for you beyond 4MB? signed 32-bit integer goes up to 2 *billion* - there must be something elsewhere limiting your size. The *default* value of MaxJsonLength is 2M Unicode chars, so 4MB. The code above is *increasing* MaxJsonLength to a *much larger* value. Maybe `int.MaxValue` is rejected as "too big" - try other sizes, and see how large you can get. – ToolmakerSteve Oct 03 '19 at 18:32
  • You know what, your are correct. I ma probably getting a flase error bubble up, or it is not utilizing the setting. I am trying to do this pre controller, not on the result. When i try to send a Json object that is large to the controller call, if i use the developer tools and debug the request server side is returning the size exceeds the Json limit. – Casey ScriptFu Pharr Oct 17 '19 at 18:58
  • This is my issue, how to get around this per Microsoft's web site. "Basically, the "internal" JavaScriptSerializer respects the value of maxJsonLength when called from a web method. Direct use of a JavaScriptSerializer (or use via an MVC action-method/Controller) does not respect the maxJsonLength property, at least not from the systemWebExtensions.scripting.webServices.jsonSerialization section which you define in the web.config file." – Casey ScriptFu Pharr Oct 17 '19 at 19:01
  • I almost wonder if I could say, break anything over 50k into chunks like List, then send that to controller, and do a string bulder, then import into SQL. – Casey ScriptFu Pharr Oct 17 '19 at 19:05
-4

What is the requirement? Are you trying to send a large SQL Table as JSON object? I think it is not practical.

You will consume a large chunk of server resource if you push thru with this. You will also not be able to measure the progress with a progress bar because your App will just wait for the server to reply back which would take ages.

What I recommend to do is to chop the request into batches say first 1000 then request again the next 1000 till you get what you need. This way you could also put a nice progress bar to know the progress as extracting that amount of data could really take too long.

-11

The maximum length of JSON strings. The default is 2097152 characters, which is equivalent to 4 MB of Unicode string data.

Refer below URL

https://learn.microsoft.com/en-us/dotnet/api/system.web.script.serialization.javascriptserializer.maxjsonlength?view=netframework-4.7.2

  • 3
    JSON itself has no inherent limit. What you posted is specific to .net and its `JavaScriptSerializer` class. And `MaxJsonLength` has already been addressed in other answers. – David Makogon Apr 09 '19 at 08:24
  • Can anyone post how to handle bigger then the .NET JavaScriptSerializer? For example, how can I send a large json to MVC controller but use Newtonsoft's serializer to handle it or is this not possible since it is built into .NET and used pre controller being hit? – Casey ScriptFu Pharr Oct 17 '19 at 18:56