I have created an SQL database using PHPmyAdmin which stores JSON files, the idea is to use these files with the Google Maps API to place markers on a map.
I am having trouble converting the JSON file to a JavaScript Object.
after retrieving the json from the database and converting it to a JS format, i assigned it to a variable.
For display purposes I have shown the way I get the String from PHP:
var jsonString ='<?php echo implode(" ", $firstJsonString); ?>'
As well as the actual string:
var jsonString = '{"address_components":[{"long_name":"London","short_name":"London","types":["locality","political"]},{"long_name":"London","short_name":"London","types":["postal_town"]},{"long_name":"Greater London","short_name":"Greater London","types":["administrative_area_level_2","political"]},{"long_name":"England","short_name":"England","types":["administrative_area_level_1","political"]},{"long_name":"United Kingdom","short_name":"GB","types":["country","political"]}],"adr_address":"<span class="locality">London</span>, <span class="country-name">UK</span>","formatted_address":"London, UK","geometry":{"location":{"lat":51.5073509,"lng":-0.12775829999998223},"viewport":{"south":51.38494009999999,"west":-0.351468299999965,"north":51.6723432,"east":0.14827100000002247}},"icon":"https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png","id":"b1a8b96daab5065cf4a08f953e577c34cdf769c0","name":"London","photos":[{"height":800,"html_attributions":["<a href="https://maps.google.com/maps/contrib/100576107799945331393/photos">Edu Sousa</a>"],"width":1200},{"height":1065,"html_attributions":["<a href="https://maps.google.com/maps/contrib/117641363844967821087/photos">Manuel Ligabue</a>"],"width":1600},{"height":1365,"html_attributions":["<a href="https://maps.google.com/maps/contrib/107957051173910448327/photos">David Sun</a>"],"width":2048},{"height":2136,"html_attributions":["<a href="https://maps.google.com/maps/contrib/110744511144480687702/photos">Nuno Martins</a>"],"width":3216},{"height":3000,"html_attributions":["<a href="https://maps.google.com/maps/contrib/118133211030153942640/photos">Roberto Gerosa</a>"],"width":4000},{"height":3024,"html_attributions":["<a href="https://maps.google.com/maps/contrib/102674303425902835965/photos">ALLWYN D'SILVA</a>"],"width":4032},{"height":1080,"html_attributions":["<a href="https://maps.google.com/maps/contrib/111050241231844820150/photos">Cristina Cunsolo</a>"],"width":1920},{"height":1362,"html_attributions":["<a href="https://maps.google.com/maps/contrib/101001561862058426912/photos">Dendy Legends</a>"],"width":2048},{"height":1440,"html_attributions":["<a href="https://maps.google.com/maps/contrib/111849553513496073867/photos">Destiny Symonee</a>"],"width":2560},{"height":1452,"html_attributions":["<a href="https://maps.google.com/maps/contrib/109648577636572315039/photos">Leslie</a>"],"width":5248}],"place_id":"ChIJdd4hrwug2EcRmSrV3Vo6llI","reference":"CmRbAAAAw49v9euZCWzrvjJ9b8STdg4qK48YueqQIOMerhQ43OP-NynPqFygwZnFzb-jcKkRKJog7z2IeUubF468akLhMj7pIZSoOWFXGjKC0WgkubgTIRlySRkBM47V9RMcX5YoEhB3qYHCQ3roB1G_gHnh_i8MGhSbxmofinNGCLquqddTEQ3DjsKOcg","scope":"GOOGLE","types":["locality","political"],"url":"https://maps.google.com/?q=London,+UK&ftid=0x47d8a00baf21de75:0x52963a5addd52a99","utc_offset":60,"vicinity":"London","html_attributions":[]} {"address_components":[{"long_name":"London","short_name":"London","types":["locality","political"]},{"long_name":"London","short_name":"London","types":["postal_town"]},{"long_name":"Greater London","short_name":"Greater London","types":["administrative_area_level_2","political"]},{"long_name":"England","short_name":"England","types":["administrative_area_level_1","political"]},{"long_name":"United Kingdom","short_name":"GB","types":["country","political"]}],"adr_address":"<span class="locality">London</span>, <span class="country-name">UK</span>","formatted_address":"London, UK","geometry":{"location":{"lat":51.5073509,"lng":-0.12775829999998223},"viewport":{"south":51.38494009999999,"west":-0.351468299999965,"north":51.6723432,"east":0.14827100000002247}},"icon":"https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png","id":"b1a8b96daab5065cf4a08f953e577c34cdf769c0","name":"London","photos":[{"height":800,"html_attributions":["<a href="https://maps.google.com/maps/contrib/100576107799945331393/photos">Edu Sousa</a>"],"width":1200},{"height":1065,"html_attributions":["<a href="https://maps.google.com/maps/contrib/117641363844967821087/photos">Manuel Ligabue</a>"],"width":1600},{"height":1365,"html_attributions":["<a href="https://maps.google.com/maps/contrib/107957051173910448327/photos">David Sun</a>"],"width":2048},{"height":2136,"html_attributions":["<a href="https://maps.google.com/maps/contrib/110744511144480687702/photos">Nuno Martins</a>"],"width":3216},{"height":3000,"html_attributions":["<a href="https://maps.google.com/maps/contrib/118133211030153942640/photos">Roberto Gerosa</a>"],"width":4000},{"height":3024,"html_attributions":["<a href="https://maps.google.com/maps/contrib/102674303425902835965/photos">ALLWYN D'SILVA</a>"],"width":4032},{"height":1080,"html_attributions":["<a href="https://maps.google.com/maps/contrib/111050241231844820150/photos">Cristina Cunsolo</a>"],"width":1920},{"height":1362,"html_attributions":["<a href="https://maps.google.com/maps/contrib/101001561862058426912/photos">Dendy Legends</a>"],"width":2048},{"height":1440,"html_attributions":["<a href="https://maps.google.com/maps/contrib/111849553513496073867/photos">Destiny Symonee</a>"],"width":2560},{"height":1452,"html_attributions":["<a href="https://maps.google.com/maps/contrib/109648577636572315039/photos">Leslie</a>"],"width":5248}],"place_id":"ChIJdd4hrwug2EcRmSrV3Vo6llI","reference":"CmRbAAAAw49v9euZCWzrvjJ9b8STdg4qK48YueqQIOMerhQ43OP-NynPqFygwZnFzb-jcKkRKJog7z2IeUubF468akLhMj7pIZSoOWFXGjKC0WgkubgTIRlySRkBM47V9RMcX5YoEhB3qYHCQ3roB1G_gHnh_i8MGhSbxmofinNGCLquqddTEQ3DjsKOcg","scope":"GOOGLE","types":["locality","political"],"url":"https://maps.google.com/?q=London,+UK&ftid=0x47d8a00baf21de75:0x52963a5addd52a99","utc_offset":60,"vicinity":"London","html_attributions":[]}';
Here I attempt to convert it to an object:
var obj = JSON.parse(jsonString);
console.log(obj);
However when run I get an error:
Uncaught SyntaxError: Unexpected token l in JSON at position 492
I checked position 492, this is at the point where there is an '=' symbol. Looking at it now, I believe this is the first occurence of an "=" symbol in this JSON string.
Why is this = symbol causing this error, and how can I prevent it?
Thanks!
$firstJsonString, and why are you callingimplode()?require_once('getMarkers.php'); $markerArray = getMarkers(); $firstJsonString = $markerArray[0];its the first string from my array of Json Strings from the database, i was imploding since If i just did $firstJsonString, it would just say "Array" instead of the desired string.