SyntaxError: JSON Parse error: Unexpected identifier "object"

16,349

Solution 1

getJSON parses the JSON for you — calling $.parseJSON will convert the object into the string [object Object] and then try to parse that, giving you an error. Just omit the $.parseJSON call and use data directly.


Furthermore, I should note that the calls to array_push are strange and unnecessary. array_push usually takes an array and a value to push on to it, but (for example) in your first line you're setting $return['amount'] to "$amount" and then passing $return['amount'] to array_push, which does nothing at best and might give you a warning or error at worst. You'd get the exact same behavior if you did this:

$return['amount']="$amount";
$return['fee']="$fee";
$return['total']="$total";

Then you might also realize that the quotes around, say, "$amount" are unnecessary, and you could actually do this:

$return['amount']=$amount;
$return['fee']=$fee;
$return['total']=$total;

Finally, you can actually condense all five lines using some special array syntax very easily:

echo json_encode(array(
    'amount' => $amount,
    'fee' => $fee,
    'total' => $total
));

This is quite a bit nicer if I do say so myself.

Solution 2

Actually u dont need to parse it. U can directly access it

$.getJSON("process.php?amount="+amount, function(data,status) {
 alert (data.amount); 
});
Share:
16,349

Related videos on Youtube

Vasvas
Author by

Vasvas

Updated on June 04, 2022

Comments

  • Vasvas
    Vasvas almost 2 years

    Please help me to understand what's wrong. I want to parse JSON reply as object.

    PHP process.php code:

    <?php
        $return = array();
            array_push($return['amount']="$amount");
            array_push($return['fee']="$fee");
            array_push($return['total']="$total");
        echo json_encode($return);
    ?>
    

    Returns JSON string:

    {"amount":"350","fee":"0","total":"350"}
    

    JS (jquery) code:

    $.getJSON("process.php?amount="+amount, function(data,status) {
       var obj = $.parseJSON(data);
       alert (obj.amount);
    });
    

    I receive error:

    SyntaxError: JSON Parse error: Unexpected identifier "object"

    BUT! When I try to insert result instead data (but insert ' quotes left/right):

    var obj = $.parseJSON('{"amount":"350","fee":"0","total":"350"}');
    

    And I see alert = 350. So, it's working good.

    I try to make something like that:

    var jsonreply = "'"+data+"'";
    var obj = $.parseJSON(jsonreply);
    

    But received below error:

    SyntaxError: JSON Parse error: Single quotes (') are not allowed in JSON

  • andrewvnice
    andrewvnice over 10 years
    Also, why are you using array_push in your PHP script? If you would like to add values to an array, you can do so more easily like so: $return['amount']=$amount;
  • icktoofay
    icktoofay over 10 years
    @mazinsights: Your comment might better belong on the question myself, as I wasn't the one who wrote the code in the first place! It's a good suggestion, though, so I've added that (and a bit more, actually) to my answer.
  • icktoofay
    icktoofay over 10 years
    ' is not a special character within double quotes; backslashing it won't do anything. Perhaps you meant "\\'", but then you're still generating invalid JSON.
  • Vasvas
    Vasvas over 10 years
    In example above I make a simple code. Really, I used array, but when I array_push one array to another I received not good results for json. Something like ["0",{"key0":"value","key1":"value"},{"key0":"value"}]
  • Vasvas
    Vasvas over 10 years
    @icktoofay: Exactly! Thank you so much!