0

I got an array from an SQL query, encoded it and when I decode in javascript I receive the error

Unexpected token a in JSON at position 0

The var_dump of the array before the encode:

array(1) { [0]=> array(10) { ["order_id"]=> string(10) "1512642144" 
["order_erp_id"]=> string(1) "0" ["order_type"]=> string(3) "web" 
["order_date"]=> string(23) "2017-12-07 11:22:24.263" ["order_total"]=> 
string(4) "0.00" ["order_desc1"]=> string(3) "web" ["order_desc2"]=> 
string(4) "test" ["no_items"]=> string(1) "0" ["order_count"]=> int(1) 
["item_arr"]=> string(0) "" } } 
[{"order_id":"1512642144","order_erp_id":"0","order_type":"web",
"order_date":"2017-12-07 11:22:24.263","order_total":"0.00",  
"order_desc1":"web","order_desc2":"test","no_items":"0",
"order_count":1,"item_arr":""}]

then I encode and send it with:

echo json_encode($order_item_arr);

On the client side, I receive the data and before the decoding step I have this from the console.log(data); in javascript:

array(1) {
[0]=>
array(10) {
["order_id"]=>
string(10) "1512642144"
["order_erp_id"]=>
string(1) "0"
["order_type"]=>
string(3) "web"
["order_date"]=>
string(23) "2017-12-07 11:22:24.263"
["order_total"]=>
string(4) "0.00"
["order_desc1"]=>
string(3) "web"
["order_desc2"]=>
string(4) "test"
["no_items"]=>
string(1) "0"
["order_count"]=>
int(1)
["item_arr"]=>
string(0) ""
  }
}


[{"order_id":"1512642144","order_erp_id":"0","order_type":"web",
"order_date":"2017-12-07 11:22:24.263","order_total":"0.00",  
"order_desc1":"web","order_desc2":"test","no_items":"0", 
"order_count":1,"item_arr":""}]

So it looks like quite ok, and when I decode it with JSON.parse(data); I have the error:

VM414:1 Uncaught SyntaxError: Unexpected token a in JSON at position 0   

3 Answers 3

5

Don't output var_dump on your PHP page, it invalidates the json on that page. Remove the var_dump() and the errors will go away.

As you can see from:

Unexpected token a in JSON at position 0

That the error is occurring due to array(1) {, which is not a valid json. And is breaking the entire json data along with it.

So, just remove the var_dump().

Look at this:

array(1) {
[0]=>
array(10) {
["order_id"]=>
string(10) "1512642144"
["order_erp_id"]=>
string(1) "0"
["order_type"]=>
string(3) "web"
["order_date"]=>
string(23) "2017-12-07 11:22:24.263"
["order_total"]=>
string(4) "0.00"
["order_desc1"]=>
string(3) "web"
["order_desc2"]=>
string(4) "test"
["no_items"]=>
string(1) "0"
["order_count"]=>
int(1)
["item_arr"]=>
string(0) ""
  }
}

Removing var_dump() will remove the above part and, only thing which will remain is the string below, which is valid json.

[{"order_id":"1512642144","order_erp_id":"0","order_type":"web",
"order_date":"2017-12-07 11:22:24.263","order_total":"0.00",  
"order_desc1":"web","order_desc2":"test","no_items":"0", 
"order_count":1,"item_arr":""}]
Sign up to request clarification or add additional context in comments.

Comments

3

Uncaught SyntaxError: Unexpected token a in JSON at position 0

So the very first character in your "JSON" is a, which can't be valid JSON. And your echo json_encode($order_item_arr); line will not output it.

The var_dump of the array before the encode:

array(1) { [0]=> array

… and there is your problem. You can see that the first character is a.

You are trying to parse your var_dump as if it were JSON.

Don't do that. Parse the JSON instead.

2 Comments

Thanks, how can I correctly encode the array without sending the 'array(1) . . .'
@StefanoMaglione — Do. Not. Use. var_dump.
0

JSON.parse() is used to convert a string containing JSON notation into a Javascript object.

Your code turns the object into a string (by calling .toString()) in order to try to parse it as JSON text. The default .toString() returns "[object Object]", which is not valid JSON; hence the error

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.