Which JSON content type do I use?

Viewed 3575350

There are many "standards" for the JSON content type:

application/json
application/x-javascript
text/javascript
text/x-javascript
text/x-json

Which one do I use, and where? I assume security and browser support issues are a factor.

Related: What MIME type if JSON is being returned by a REST API?

38 Answers

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 relevant comments:

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.

As you may have to use these more frequently, always try to remember these three content types even though there are many content types:

  • Content-Type: application/json
  • Content-Type: application/xml
  • Content-Type: text/html

As some research,

The most common MIME type is

application/json

Let's see a example to differentiate with JSON and JavaScript.

  • application/json

It is used when it is not known how this data will be used. When the information is to be just extracted from the server in JSON format, it may be through a link or from any file, in that case, it is used.

For example-

<?php

    header('Content-type:application/json');

    $directory = [
            ['Id' => 1, 'Name' => 'this'],
            ['Id' => 2, 'Name' => 'is'],
            ['Id' => 3, 'Name' => 'Stack Overflow'],
        ];

    // Showing the JSON data

    echo json_encode($directory);
?>

The output is,

[{"Id":1, "Name":"this"}, {"Id":2, "Name":"is"}, {"Id":3, "Name":"Stack Overflow"}]

  • application/javascript

It is used when the use of the data is predefined. It is used by applications in which there are calls by the client-side Ajax applications. It is used when the data is of type JSON-P or JSONP.

For example

<?php

    header('Content-type:application/javascript');

    $dir = [
            ['Id' => 1, 'Name' => 'this' ],
            ['Id' => 2, 'Name' => 'is'],
            ['Id' => 3, 'Name' => 'Stack Overflow'],
    ];

    echo "Function_call(" . json_encode($dir) . ");";
?>

The output is,

Function_call([{"Id":1, "Name":"this"}, {"Id":2, "Name":"is"}, {"Id":3, "Name":"Stack Overflow"}])

And for other MIME types, see the full detail in MIME types (IANA media types).

The most common MIME type is application/json. Here is a list of all JSON content types:

  • Content-Type: application/json - JSON
  • Content-Type: text/x-json - JSON before application/json got officially registered.
  • Content-Type: application/javascript - JSON-P
  • Content-Type: application/x-javascript - JavaScript

Obsolete Types:

  • Content-Type: text/javascript - JavaScript but obsolete. Older Internet Explorer versions used to use it for HTML attributes.
  • Content-Type: text/x-javascript - JavaScript Media Types, but obsolete

A part of your question is relevant to me as I just came across it.

A third-party provider is providing a REST service that is used by multiple clients. It's a straight-forward REST called with query parameters that returns a well-formed JSON. I have tested it with PHP and Java where it worked as expected.

My client uses Oracle Service Bus as a gateway between his application server and the Internet. When I made the OSB service, it crashed with an Invalid message format error. Turned out that the content-type being returned was text/html. OSB treats responses as per this header; converting between text, XML and JSON. In this case, the response was JSON but the header didn't say so. Contacting the provider, I got the reply: "We're not going to change it as it doesn't effect anyone else".

The Content-Type header specifies what the content should be, not what it actually is. That is to say, in your consuming program, it's up to you to check or ignore it and process the content in any manner. Another example, you can return GIF data but specify the content type as JSON, then go ahead and ignore the header and read the image data. This won't hurt your program, but may hurt others.

Moral of the story: Play nice.

It depends on the point of view.

If you are the client sending a request, then application/json is the right choice.

But if you are the server receiving a request, you have to be prepared, that the client may also send the encoding. So application/json and application/json; charset=utf-8 are valid.

The media type is the same in both cases. But the content type differs.

Related