0

I've got a platform that allows users to output selected data into a specific JSON schema. The code is pretty straight forward, and essentially after the SQL query I create an array, start gathering the required information and then populating.

$rowarray['id'] = $orgprefix.'-'.$row['ContractID'];
$rowarray['title'] = $row['Service_Name'];
$rowarray['description'] = $row['Description'];
etc etc

In terms of output, I simply use

echo json_encode($dbdata,JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);

Anyway, all was working well, until someone mentioned that they were getting totally blank output on one of their budgets. After some serious head scratching, I realised that whenever data from the DB includes a £ sign, the entire process fails.

Now obviously, I can look at options to escape data before adding it to an array, but is there a list of characters I need to be aware of when building JSON data? I was also thinking, is this some sort of encoding issue?

  • 1
    Yes, that most likely is an encoding issue. Use `json_last_error`/`json_last_error_msg` to verify that first of all (instead of guessing.) – CBroe Jul 23 '20 at 10:26
  • 1
    And after that, this question probably qualifies as a duplicate of one of the SO all-time classics, https://stackoverflow.com/questions/279170/utf-8-all-the-way-through – CBroe Jul 23 '20 at 10:27
  • Thank you. I'm not that familiar with working with JSON. Yes, it was an encoding issue. All fixed now. –  Jul 23 '20 at 11:31

0 Answers0