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:
application/json
application/x-javascript
text/javascript
text/x-javascript
text/x-json
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.
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.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, sotext/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 viatext/plain
?But honestly, your choice of MIME type is advice to the client as to how to interpret the data-
text/plain
ortext/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!
“
application/json
” is the correct JSON content type.The right MIME type is
application/json
BUT
I experienced many situations where the browser type or the framework user needed:
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
andapplication/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.