12

I have an Apache server which is serving out static JSON files. The Content Type is correctly being represented in the header with an Content-Type: application/json header.

How do I add the correct Charset to the header?

I have the following line in my httpd.conf:

AddDefaultCharset utf-8

but even with this line, Apache does not set the charset in the header.

This is important, because my JSON files have some data in French, and the Accents are not being properly read in the JavaScript Code.

So how do I set proper Headers for JSON in Apache?

3
  • How are your JSON-files encoded? Because JSON HAS to be either UTF-8, UTF-16 or UTF-32 anything else is not allowed and just sending a different Charset will not automagically convert the files.
    – lsmooth
    Mar 13, 2014 at 11:16
  • The JSON is encoded in UTF-8 Mar 13, 2014 at 14:02
  • @lsmooth The file being encoded with UTF-8 is not enough in some circumstances, especially involving IE9 (possibly earlier, too); see here. NSFW language, unsurprisingly :-). Personally, I've encountered issues with jquery-datatables and JSON without charset headers when testing with IE9.
    – JK Laiho
    Mar 18, 2015 at 13:15

2 Answers 2

12

You can use the following code in your httpd.conf if you have access, or even your .htaccess , to force Apache, to send an UTF-8 encoding header.

AddDefaultCharset utf-8
AddType 'application/json; charset=UTF-8' .json
1
  • I was searching for a way to make Apache serve static files with UTF-8 encoding, AddType finally did this. Thanks! Nov 25, 2016 at 18:11
1

You can add this to httpd.conf

AddCharset utf8 .json
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.