I have PostgreSQL database that contains columns with string values that contain double quotes.
Example value:
Exploitation field of hydrocarbons "Environment"
I use PHP to fetch contents of the table that contains this record and store query result to $queryResult
variable.
I use json_encode()
to convert query result to JSON like this:
$result = pg_query($query);
$queryResult = array();
while ($row = pg_fetch_row($result)) {
$queryResult[] = $row;
}
$queryResult_json = json_encode($queryResult);
I want to use this as JS object so I send data to JavaScript like this:
<script type="text/javascript">
var myArray = '<?php echo $queryResult_json ?>';
myArray_object = JSON.parse(myArray);
</script>
The problem is, I get an error in console:
Uncaught SyntaxError: Unexpected token O in JSON at position 9163
and it looks like it points to double quotes inside my string beacause JSON uses double quotes around the values so it cannot parse double quote inside double quote.
The part of string that JSON.parse() is having trouble with looks like this:
["0134","Cadastral plan - Exploitation field of hydrocarbons "Environment" - WFS"]
I cannot use .replace() method because it would replace all double quotes, not just the ones inside the string. Is there a way to work around this?