[Solved] PHP json_encode json_decode UTF-8

How can I save a json-encoded string with international characters to the databse and then parse the decoded string in the browser?

    $string = "très agréable";  
    // to the database 
    $j_encoded = json_encode(utf8_encode($string)); 
    // get from Database 
    $j_decoded = json_decode($j_encoded); 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<html xmlns="" xml:lang="fr" lang="fr">
    <?= $j_decoded ?>
Enquirer: FFish


Solution #1:

This is an encoding issue. It looks like at some point, the data gets represented as ISO-8859-1.

Every part of your process needs to be UTF-8 encoded.

  • The database connection

  • The database tables

  • Your PHP file (if you are using special characters inside that file as shown in your example above)

  • The content-type headers that you output

Respondent: Pekka

Solution #2:

json utf8 encode and decode:

json_encode($data, JSON_UNESCAPED_UNICODE)

json_decode($json, false, 512, JSON_UNESCAPED_UNICODE)

force utf8 might be helpfull too:

Respondent: Lukas Liesis

Solution #3:

  header('Content-Type: application/json; charset=utf-8');

Solution #4:

If your source-file is already utf8 then drop the utf8_* functions.
php5 is storing strings as array of byte.

you should add a meta tag for encoding within the html AND you should add an http header which sets the transferencoding to utf-8.

   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

and in php

header('Content-Type: text/html; charset=utf-8');
Respondent: Bernd Ott

Solution #5:

Try sending the UTF-8 Charset header:

<?php header ('Content-type: text/html; charset=utf-8'); ?>

And the HTML meta:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Respondent: Coquevas

Solution #6:

  1. utf8_decode $j_decoded = utf8_decode(json_decode($j_encoded)); EDIT
    or to be more correct $j_encoded = json_encode($j_encoded); $j_decoded = json_decode($j_encoded); no need for en/decoding utf8
  2. <meta charset="utf-8" />
Respondent: teemitzitrone

Solution #7:

For me both methods


header('Content-Type: text/html; charset=utf-8');

echo json_encode($YourData, JSON_UNESCAPED_UNICODE);

Solution #8:

if you get “unexpected Character” error you should check if there is a BOM (Byte Order Marker saved into your utf-8 json.
You can either remove the first character or save if without BOM.

Respondent: Blox

Solution #9:

Work for me 🙂

function jsonEncodeArray( $array ){
    array_walk_recursive( $array, function(&$item) { 
       $item = utf8_encode( $item ); 
    return json_encode( $array );
Respondent: Douglas Comim

Solution #10:

I had the same problem. It might differ depending on how You put the data to the db, but try what worked for me:

$str = json_encode($data);
$str = addslashes($str);

Do this before saving data to db.

Respondent: Micha? Moskal

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy