Link to home
Start Free TrialLog in
Avatar of James Murrell
James MurrellFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

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.
Avatar of James Murrell

ASKER

code in in php

include("members_area/db.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
Hülkenberg
Räikkönen
Gutiérrez
Pérez

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

ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks your way worked:


plus i tried mysql_query("SET NAMES 'utf8'"); and that worked to thanks
This article may be helpful in understanding the character encoding issues:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11880-Unicode-PHP-and-Character-Collisions.html

JSON requires valid UTF-8, and that's the direction the world is taking.  You might want to consider converting the database, too.
http://iconoun.com/articles/collisions/Unicode_Presentation.pdf
Just a thought, you might want to check out the difference between mysql_query("SET NAMES 'utf8'"); and mysql_query("SET NAMES 'utf8mb4'");
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
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