We help IT Professionals succeed at work.
Get Started

Working with JSON from MSQL that has an apostrophe

lvmllc asked
Last Modified: 2015-06-18
I am retrieving values from MySQL with PHP for use in a mapping application all works unless my data has an apostrophe in it.

if ($result = $mysqli->query($query)) {
		while ($row = $result->fetch_assoc()) {
			$json[] = ($row);
echo "var locations = '".json_encode($json)."'";

Open in new window

the JavaScript parsing so I can use this data in a Leaflet Map is as follows

var parsedLocations = JSON.parse(locations);
i = 0;
while (i < parsedLocations.length) {
    var marker = L.marker([parsedLocations[i].latitude, parsedLocations[i].longitude], {
        title: parsedLocations[i].title,
        opacity: 0.9,
        riseOnHover: true

Open in new window

So all works unless there is an apostrophe or other special character in the JSON output
Watch Question
Top Expert 2014
This problem has been solved!
Unlock 1 Answer and 18 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE