What is the correct JSON content type?


I've been messing around with JSON for some time, just pushing it out as text and it hasn't hurt anybody (that I know of), but I'd like to start doing things properly.



I have seen so many purported "standards" for the JSON content type:



But which is correct, or best? I gather that there are security and browser support issues varying between them.



I know there's a similar question, What MIME type if JSON is being returned by a REST API?, but I'd like a slightly more targeted answer.



For JSON text:



application/json



The MIME media type for JSON text is application/json. The default encoding is UTF-8. (Source: RFC 4627).



For JSONP (runnable javascript) with callback:



application/javascript



Here are some blog posts that were mentioned in the comments that are relevant.



IANA has registered the official MIME Type for JSON as application/json.



When asked about why not text/json, Crockford seems to have said JSON is not really JavaScript nor text and also IANA was more likely to hand out application/* than text/*.



More resources:



For JSON:



For JSON-P:



Of course, the correct MIME media type for JSON is application/json, but it's necessary to realize what type of data is expected in your application.



For example, I use Ext GWT and the server response must go as text/html but contains JSON data.



Client side, Ext GWT form listener



In case of using application/json response type, the browser suggests me to save the file.



Server side source code snippet using Spring MVC



Response is dynamically generated data, according to the query parameters passed in the URL.



Example:



Content-Type: application/json



JSON with padding.
Response is JSON data, with a function call wrapped around it.



Example:



Content-Type: application/javascript



If you are using Ubuntu or Debian and you serve .json files through Apache, you might want to serve the files with the correct content type. I am doing this primarily because I want to use the Firefox extension JSONView



The Apache module mod_mime will help to do this easily. However, with Ubuntu you need to edit the file /etc/mime.types and add the line



Then restart Apache:



If you're calling ASP.NET Web Services from the client-side you have to use application/json for it to work. I believe this is the same for the jQuery and Ext frameworks.



The right content type for JSON is application/json UNLESS you're using JSONP, also known as JSON with Padding, which is actually JavaScript and so the right content type would be application/javascript.



There is no doubt that application/json is the best MIME type for a JSON response.



But I had some experience where I had to use application/x-javascript because of some compression issues. My hosting environment is shared hosting with GoDaddy. They do not allow me to change server configurations. I had added the following code to my web.config file for compressing responses.



By using this, the .aspx pages was compressed with g-zip but JSON responses were not. I added



in the static and dynamic types sections. But this does not compress JSON responses at all.



After that I removed this newly added type and added



in both the static and dynamic types sections, and changed the response type in



.ashx (asynchronous handler) to



And now I found that my JSON responses were compressed with g-zip. So I personally recommend to use



only if you want to compress your JSON responses on a shared hosting environment. Because in shared hosting, they do not allow you to change IIS configurations.



Only when using application/json as the MIME type I have the following (as of November 2011 with the most recent versions of Chrome, Firefox with Firebug):



Not everything works for content type application/json.



If you are using Ext JS form submit to upload file, be aware that the server response is parsed by the browser to create the document for the <iframe>.



If the server is using JSON to send the return object, then the Content-Type header must be set to text/html in order to tell the browser to insert the text unchanged into the document body.



See the Ext JS 3.4.0 API documentation.



JSON is a domain-specific language (DSL) and a data format independent of JavaScript, and as such has its own MIME type, application/json. Respect for MIME types is of course client driven, so text/plain may do for transfer of bytes, but then you would be pushing up interpretation to the vendor application domain unnecessarily - application/json. Would you transfer XML via text/plain?



But honestly, your choice of MIME type is advice to the client as to how to interpret the data- text/plain or text/HTML (when it's not HTML) is like type erasure- it's as uninformative as making all your objects of type Object in a typed language.



No browser runtime I know of will take a JSON document and automatically make it available to the runtime as a JavaScript accessible object without intervention, but if you are working with a crippled client, that's an entirely different matter. But that's not the whole story- RESTful JSON services often don't have JavaScript runtimes, but it doesn't stop them using JSON as a viable data interchange format. If clients are that crippled... then I would consider perhaps HTML injection via an Ajax templating service instead.



Application/JSON!



If you're in a client-side environment, investigating about the cross-browser support is mandatory for a well supported web application.



The right HTTP Content-Type would be application/json, as others already highlighted too, but some clients do not handle it very well, that's why jQuery recommends the default text/html.



The correct answer is:



As many others have mentioned, application/json is the correct answer.



But what haven't been explained yet is what the other options you proposed mean.



application/x-javascript: Experimental MIME type for JavaScript before application/javascript was made standard.



text/javascript: Now obsolete. You should use application/javascript when using javascript.



text/x-javascript: Experimental MIME type for the above situation.



text/x-json: Experimental MIME type for JSON before application/json got officially registered.



All in all, whenever you have any doubts about content types, you should check this link



In JSP, you can use this in page directive:



The correct MIME media type for JSON is application/json. JSP will use it for sending a response to the client.



application/json” is the correct JSON content type.



The IANA registration for application/json says



Applications that use this media type: JSON has been used to
exchange data between applications written in all of these
programming languages: ActionScript, C, C#, Clojure, ColdFusion,
Common Lisp, E, Erlang, Go, Java, JavaScript, Lua, Objective CAML,
Perl, PHP, Python, Rebol, Ruby, Scala, and Scheme.



You'll notice that IANA.org doesn't list any of these other media types, in fact even application/javascript is now obsolete. So application/json is really the only possible correct answer.



Browser support is another thing.



The most widely supported non-standard media types are text/json or text/javascript. But some big names even use text/plain.



Even more strange is the Content-Type header sent by Flickr, who returns JSON as text/xml. Google uses text/javascript for some of it's ajax apis.



Examples:



Output: Content-Type: text/javascript



Output: Content-Type: text/xml



The right MIME type is application/json



BUT



I experienced many situations where the browser type or the framework user needed:



I use the below



The Content-Type header should be set to 'application/json' when posting. Server listening for the request should include "Accept=application/json".
In Spring MVC you can do it like this:



Add headers to the response:



In Spring you have a defined type: MediaType.APPLICATION_JSON_VALUE which is equivalent to application/json.



The application/json works great in PHP to store an array or object
data.



I use this code to put data in JSON on Google Cloud Storage (GCS) which is set publically viewable:



To get back the data is straight forward:



If the JSON is with padding then it will be application/jsonp. If the JSON is without padding then it will be application/json.



To deal with both, it is a good practice to use: 'application/javascript' without bothering whether it is with padding or without padding.



For JSON, I am using:



This is described in the IETF's JSON Data Interchange Format 7158 proposal, Section 1.2: Specifications of JSON.



PHP developers use this:



Extending the accepted responses, when you are using JSON in a REST context...



There is a strong argument about using application/x-resource+json and application/x-collection+json when you are representing REST resources and collections.



And if you decide to follow the jsonapi specification, you should use of application/vnd.api+json, as it is documented.



Altough there is not an universal standard, it is clear that the added semantic to the resources being transfered justify a more explicit Content-Type than just application/json.



Following this reasoning, other contexts could justify a more specific Content-Type.



If you get data from REST API in JSON so you have to use content-type



JSON (JavaScript Object Notation) and JSONP ("JSON with padding") formats seems to be very similar and therefor it might be very confusing which MIME type they should be using. Even though the formats seems to be very similar, there are some subtle differences between them.



So whenever in any doubts, I have very simple approach (which works perfectly find in most cases), namely, go and check corresponding RFC document.



JSON
RFC 4627 (The application/json Media Type for JavaScript Object Notation (JSON)) is a specifications of JSON format. It says in section 6, that the MIME media type for JSON text is



JSONP
JSONP ("JSON with padding") is handled different way than JSON, in a browser. JSONP is treated as a regular JavaScript script and therefor it should use application/javascript, the current official MIME type for JavaScript. In many cases, however, text/javascript MIME type will work fine too.



Note that text/javascript has been marked as obsolete by RFC 4329 (Scripting Media Types) document and it is recommended to use application/javascript type instead. However, due to legacy reasons, text/javascript is still widely used and it has cross-browser support (which is not always a case with application/javascript MIME type, especially with older browsers).



Content-type: application/json - json

Content-Type: application/javascript - json-P

Content-type: application/x-javascript - javascript

Content-type: text/javascript - javascript BUT obsolete, older IE versions used to use as html attribute.

Content-type: text/x-javascript - JavaScript Media Types BUT obsolete

Content-type: text/x-json - json before application/json got officially registered.




Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).


Would you like to answer one of these unanswered questions instead?

Popular posts from this blog

The Dalles, Oregon

眉山市

清晰法令