I have some json I need to decode

I have some json I need to decode, alter and then encode without messing up any characters.

If I have a unicode character in a json string it will not decode.

so my code in php creates json in correct fromat except for one thing...

in my database i have "Sergio Pérez"

and it is comming through json as null    
"surname":null,"Construtor":"Force India

Open in new window

LVL 31
James MurrellProduct SpecialistAsked:
Who is Participating?
gr8gonzoConnect With a Mentor ConsultantCommented:
Try utf8_encode()-ing the values first:

while ($raceResult = mysql_fetch_array($result, MYSQL_ASSOC)) {
   $raceResult["surname"] = utf8_encode($raceResult["surname"]);
   $raceData['results'][] = $raceResult;
JSON encoding/decoding should work fine with unicode. Are you certain the problem is not occurring before the encoding happens? Are you maybe trying to perform single-byte character operations like str_replace() on values that could contain multi-byte characters?
Please provide a full example, showing the sample value, your script that accepts the value, and the script that encodes/decodes.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

James MurrellProduct SpecialistAuthor Commented:
code in in php

//$sql = sprintf("select * from races where raceID = '%s';", $_POST['F1']);
$sql = sprintf("SELECT d.surname, con.name AS Construtor, res.positionOrder FROM  races AS ra INNER JOIN results AS res ON ra.raceId = res.raceId INNER JOIN  seasons AS s ON ra.year = s.year INNER JOIN  drivers AS d ON res.driverId = d.driverId   INNER JOIN constructors AS con ON res.constructorId = con.constructorId  GROUP BY s.year , ra.round , ra.name , ra.date , d.surname , con.name , res.points ,ra.raceID, res.positionOrder having ra.raceID = '%s' ORDER BY ra.round DESC , res.positionOrder ;", $_POST['F1']);

$result = mysql_query($sql);
//var_dump( mysql_fetch_array($result) );
$raceData = array();
while ($raceResult = mysql_fetch_array($result, MYSQL_ASSOC)) {
   $raceData['results'][] = $raceResult;
echo json_encode( $raceData );
//var_dump( $raceData );

Open in new window

all these come out of database as

but in json it says null

full json is

{"results":[{"surname":"Hamilton","Construtor":"Mercedes","positionOrder":"1"},{"surname":"Rosberg","Construtor":"Mercedes","positionOrder":"2"},{"surname":"Vettel","Construtor":"Red Bull","positionOrder":"3"},{"surname":"Alonso","Construtor":"Ferrari","positionOrder":"4"},{"surname":null,"Construtor":"Force India","positionOrder":"5"},{"surname":"Button","Construtor":"McLaren","positionOrder":"6"},{"surname":"Massa","Construtor":"Williams","positionOrder":"7"},{"surname":"Bottas","Construtor":"Williams","positionOrder":"8"},{"surname":"Magnussen","Construtor":"McLaren","positionOrder":"9"},{"surname":"Kvyat","Construtor":"Toro Rosso","positionOrder":"10"},{"surname":"Grosjean","Construtor":"Lotus F1","positionOrder":"11"},{"surname":null,"Construtor":"Ferrari","positionOrder":"12"},{"surname":"Kobayashi","Construtor":"Caterham","positionOrder":"13"},{"surname":"Ericsson","Construtor":"Caterham","positionOrder":"14"},{"surname":"Chilton","Construtor":"Marussia","positionOrder":"15"},{"surname":"Ricciardo","Construtor":"Red Bull","positionOrder":"16"},{"surname":null,"Construtor":"Sauber","positionOrder":"17"},{"surname":"Sutil","Construtor":"Sauber","positionOrder":"18"},{"surname":"Vergne","Construtor":"Toro Rosso","positionOrder":"19"},{"surname":"Bianchi","Construtor":"Marussia","positionOrder":"20"},{"surname":"Maldonado","Construtor":"Lotus F1","positionOrder":"21"},{"surname":null,"Construtor":"Force India","positionOrder":"22"}]}

Open in new window

James MurrellProduct SpecialistAuthor Commented:
thanks your way worked:

plus i tried mysql_query("SET NAMES 'utf8'"); and that worked to thanks
Ray PaseurCommented:
This article may be helpful in understanding the character encoding issues:

JSON requires valid UTF-8, and that's the direction the world is taking.  You might want to consider converting the database, too.
Ray PaseurCommented:
Just a thought, you might want to check out the difference between mysql_query("SET NAMES 'utf8'"); and mysql_query("SET NAMES 'utf8mb4'");
Chris StanyonCommented:
Hey James,

Only just got back to the PC, and you've already solved this one. Basically the reason you were having problems is because at least one of your layers is not set up for UTF-8 encoding. As you've figured out - it's the database. When you create tables in mySQL, set them to UTF-8 encoding at that stage and it will avoid this issue.

Basically, by calling SET NAMES you're saying to mySQL - give me the data back in UTF-8 encoding no matter how it's stored, but you'll need to do this with every query you run. Gonzo's solution performs the same task, just after the data has come back (now I've got the data, UTF encode it). Again, something you'll have to do each time you retrieve data from the database.

To avoid this, you need to make sure each layer is set to UTF-8 right from the start. The HTML page template I showed you earlier was set to UTF-8, although the page you now have online is back to iso-8859-1. Try to make sure all your layers are set to the same, and if you're planning on using non-latin characters, UTF-8 is the way to go.

If you want to change your database (and your should), run the following queries:

ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

I would also suggest you change the encoding of your HTML page back to UTF-8
James MurrellProduct SpecialistAuthor Commented:
Hi thanks for the extra help

1. changing the format back to UTF-8 made no difference to problem but will be changing it back later.
2. the database is not mine and t supplied by  Ergast Developer API  http://ergast.com/mrd/ so i cannot change
3. utf8mb4 i will look this up later today (thanks)

Once again Thanks
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.