json_encode function: special characters

PhpJson

Php Problem Overview


Elements of an array containing special characters are converted to empty strings when encoding the array with json_encode:

$arr = array ( "funds" => "ComStage STOXX®Europe 600 Techn NR ETF", "time"=>....);
$json = json_encode($arr);

After JSON encoding the element [funds] is null. It happens only with special characters (copyright, trademark etc) like the ones in "ComStage STOXX®Europe 600 Techn NR ETF".

Any suggestions?

Thanks

UPDATE: This is what solved the problem prior to populating the array (all names are taken from the db):

$mysqli->query("SET NAMES 'utf8'");

Php Solutions


Solution 1 - Php

The manual for json_encode specifies this:

> All string data must be UTF-8 encoded.

Thus, try array_mapping utf8_encode() to your array before you encode it:

$arr = array_map('utf8_encode', $arr);
$json = json_encode($arr);

// {"funds":"ComStage STOXX\u00c2\u00aeEurope 600 Techn NR ETF"}

For reference, take a look at the differences between the three examples on this fiddle. The first doesn't use character encoding, the second uses htmlentities and the third uses utf8_encode - they all return different results.

For consistency, you should use utf8_encode().

> Docs

Solution 2 - Php

Your input has to be encoded as UTF-8 or ISO-8859-1.

http://www.php.net/manual/en/function.json-encode.php

Because if you try to convert an array of non-utf8 characters you'll be getting 0 as return value.


Since 5.5.0 The return value on failure was changed from null string to FALSE.

Solution 3 - Php

To me, it works this way:

# Creating the ARRAY from Result.
$array=array();

while($row = $result->fetch_array(MYSQL_ASSOC))
{
	# Converting each column to UTF8
	$row = array_map('utf8_encode', $row);
	array_push($array,$row);
}

json_encode($array);

Solution 4 - Php

you should use this code:

$json = json_encode(array_map('utf8_encode', $arr))

array_map function converts special characters in UTF8 standard

Solution 5 - Php

Use the below function.

function utf8_converter($array)
{
    array_walk_recursive($array, function (&$item, $key) {
        if (!mb_detect_encoding($item, 'utf-8', true)) {
                $item = utf8_encode($item);
        }
    });

    return $array;
}

Solution 6 - Php

To avoid escaping of special characters, I'm passing the flag:

> JSON_UNESCAPED_UNICODE

Like this:

json_encode($array, JSON_UNESCAPED_UNICODE)

Solution 7 - Php

Use this code mysql_set_charset("UTF8", $connection);

Example $connection = mysql_connect(DB_HOST_GATEWAY,DB_USER_GATEWAY, DB_PASSWORD_GATEWAY); mysql_set_charset("UTF8", $connection); mysql_select_db(DB_DATABASE_GATEWAY,$connection);

Solution 8 - Php

you should add charset=UTF-8 in meta tag and use json_encode for special characters

$json = json_encode($arr);

json_encode function converts special characters in UTF8 standard

Solution 9 - Php

To fix the special character issue you just have to do 2 things

1.mysql_set_charset('utf8'); // set this line on top of your page in which you are using json.

  1. If you are saving json data in database make sure that the particular column collation is set to "latin1_swedish_ci".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2723490View Question on Stackoverflow
Solution 1 - PhpscrowlerView Answer on Stackoverflow
Solution 2 - PhpdeW1View Answer on Stackoverflow
Solution 3 - PhpsandolkakosView Answer on Stackoverflow
Solution 4 - Phpuser3711086View Answer on Stackoverflow
Solution 5 - PhpLakin MohapatraView Answer on Stackoverflow
Solution 6 - PhpaumanetsView Answer on Stackoverflow
Solution 7 - PhpPadmanabanView Answer on Stackoverflow
Solution 8 - Phpuser3392486View Answer on Stackoverflow
Solution 9 - PhpkavitaView Answer on Stackoverflow