JSON.parse() not working with variable type string but it works with its content if used directly

Related searches

I am getting a string from a php page using a simple code that I have used many times for obtaining string responses from php files. this is the first time I am trying to convert this result in json object (or array).

When I use JSON.parse on the response, I get the error shown below. If I pass as parameter of JSON.parse() the text that I have consoled log (i.e copied from the console as it is passed to javascript) it works perfectly.

    xmlhttp=new XMLHttpRequest();
  } else {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  xmlhttp.onreadystatechange=function() {
    if (this.readyState==4 && this.status==200) {
        var result = this.responseText;

        resultsObj = JSON.parse(result);

the code php is the following:

if (!session_id()) {
header('Content-Type: text/json');
require 'controlleur/connexionDB.php';
$sql = "SELECT * FROM depart";

$result = $conn->query($sql);
$liste .= "[";
$compteur = 0;

while ($row = $result->fetch()) {
    if ($compteur != 0) {
        $liste .= ", ";
    $liste .= "{ \"idDepart\" : \"$row->idDepart\", \"idCircuit\" : \"$row->idCircuit\", \"dateDebut\" : \"$row->dateDebut\", \"nbPlaces\" : \"$row->nbPlaces\", \"prix\" : \"$row->prix\", \"titrePromotion\" : \"$row->titrePromotion\", \"rabais\" : \"$row->rabais\" }";
$liste .= "]";
$reponse = $liste;

echo $reponse;

I am getting this error: SyntaxError: JSON.parse: unexpected character at line 1 column 1

the result in the console for the variable "result" is like this:

{ "idDepart" : "1", "idCircuit" : "5", "dateDebut" : "2019-06-02", "nbPlaces" : "30", "prix" : "4000", "titrePromotion" : "vfv", "rabais" : "10" }, 
{ "idDepart" : "2", "idCircuit" : "5", "dateDebut" : "2019-06-10", "nbPlaces" : "30", "prix" : "6000", "titrePromotion" : "ded", "rabais" : "4" }, 
{ "idDepart" : "3", "idCircuit" : "5", "dateDebut" : "2019-07-02", "nbPlaces" : "30", "prix" : "7000", "titrePromotion" : "ded", "rabais" : "6" }

JSON.parse(), The JSON.parse() method parses a JSON string, constructing the JavaScript and returns // a JavaScript value if the text is a valid JSON text. var j; function walk (holder, key) { // The walk method is used to recursively walk the We split the second stage into 4 regexp operations in order to work around� The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.

can you try simplifying the php script to this instead?

if (!session_id()) {

header('Content-Type: application/json');
require 'controlleur/connexionDB.php';
$sql = "SELECT * FROM depart";

$result = $conn->query($sql);

$liste = array();
while($row = $result->fetch()){
  $list[] = [
    "idDepart" => $row->idDepart,
    "idCircuit" => $row->idCircuit,
    "dateDebut" => $row->dateDebut, 
    "nbPlaces" => $row->nbPlaces, 
    "prix" => $row->prix, 
    "titrePromotion" => $row->titrePromotion, 
    "rabais" => $row->rabais

echo json_encode($liste);

rather than manually writing the strings, i'm using json_encode() which i think is better to avoid typos.

SyntaxError: JSON.parse: bad parsing, This string has to be valid JSON and will throw this error if incorrect syntax was encountered. Examples. JSON.parse() does not allow trailing� The JSON.parse() function is included in all major browsers and in the latest ECMAScript (JavaScript) standard. The numbers in the table below specifies the first browser version that fully supports the JSON.parse() function:

make sure, you set the header correctly on your php script

<?php header('Content-Type: text/json');

How To Work with JSON in JavaScript, Because JSON is derived from the JavaScript programming you have a JSON object in a .js or .html file, you'll likely see it set to a variable: to JSON, but the keys in JavaScript objects are not strings in quotes. Later, you can then read the information with the JSON.parse() method and work with the� To convert the example in the JSON.stringify() section above, we would pass the string s to the function, and assign it to a new variable: var o = JSON.parse(s) Then, we would have the object o to work with, which would be identical to the object obj. To take a deeper look, let’s consider an example of JSON.parse() within the context of an

JSON methods, toJSON, As we can see, toJSON is used both for the direct call JSON.stringify(room) and when room is nested in another encoded object. JSON.parse. To� Both functions only works with UTF-8 encoded string data. Encoding JSON Data in PHP. In PHP the json_encode() function is used to encode a value to JSON format. The value being encoded can be any PHP data type except a resource, like a database or file handle.

JSON object as String in query variables � Issue #262 � graphql , Can you please guide on how to pass below JSON object as String { "filter": Skip to content The above expression is a direct Mongoose schema that works on DB. call JSON.parse() on the string variable to get your unstructured JSON This still has the problem of GraphiQL highlighting my variables� Before you can add actions for creating and working with variables, your logic app must start with a trigger. Initialize variable. You can create a variable and declare its data type and initial value - all within one action in your logic app. You can only declare variables at the global level, not within scopes, conditions, and loops.

I used auto-generated schema and everything had been working just fine until a connector I used had started to return null values for strings. The fix is quite easy. You just need to manually modify a type of property in the schema.

  • What character is at line 1, column 1 ?
  • that's all that's there
  • Can you show your server side code which return json??
  • Is the character an apostrophe ?
  • You can build an array in php with all the items that you want, and then use json_enocde on that, or build one item at a time and use json_enocde on that, if you enclose it inside "[" and "]".
  • works nicely, thank you for showing me how to do this. You should include the "," after each line.
  • Yes, that true :) please check also my edit, that may help you
  • thank you some much, I will save your answer for future reference on this
  • yeah, basically like what Yanis-git answered
  • It's application/json and won't make a difference if the code (including the code in the question) doesn't pay any attention to the header.