#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    49
    Rep Power
    6

    Syntax error, malformed JSON


    Nice to meet all!
    I have such a problem. In php file I prepare json data to insert to html field
    PHP Code:
    <?php if(count($product['option'])>0){
                    
    $encoded json_encode($product['option'], true);
                  
    ?>
                   <td class="right"> <input type="checkbox" name="delete[]" value = '<?php echo $encoded?>' > </td>            
                    <?php ?>
    I can see it in browser source like
    Code:
    <td class="right"> <input type="checkbox" name="delete[]" value = '[{"name":"\u041a\u0430\u0440\u0442\u043e\u043d","value":"\u0411\u0435\u043b\u044b\u0439 \u043c\u0430\u0442\u043e\u0432\u044b\u0439","type":"select","product_id":"3701","quantity":"1","product_option_value_id":"5663"}]' > </td>
    Then I process received post data in php file
    PHP Code:
     foreach ($data['delete'] as $string) {
             echo 
    'Decoding: ' $string;
      
        
    json_decode($stringtrue);

        switch (
    json_last_error()) {
            case 
    JSON_ERROR_NONE:
                echo 
    ' - No errors';
            break;
            case 
    JSON_ERROR_DEPTH:
                echo 
    ' - Maximum stack depth exceeded';
            break;
            case 
    JSON_ERROR_STATE_MISMATCH:
                echo 
    ' - Underflow or the modes mismatch';
            break;
            case 
    JSON_ERROR_CTRL_CHAR:
                echo 
    ' - Unexpected control character found';
            break;
            case 
    JSON_ERROR_SYNTAX:
                echo 
    ' - Syntax error, malformed JSON';
            break;
            case 
    JSON_ERROR_UTF8:
                echo 
    ' - Malformed UTF-8 characters, possibly incorrectly encoded';
            break;
            default:
                echo 
    ' - Unknown error';
            break;
        }

        echo 
    PHP_EOL;
    }
         exit; 
    And in browser I get something like that
    PHP Code:
    Decoding: [{"product_id":"3659","quantity":"2"}] - Syntax errormalformed JSON Decoding: [{"name":"\u041a\u0430\u0440\u0442\u043e\u043d","value":"\u0411\u0435\u043b\u044b\u0439 \u043c\u0430\u0442\u043e\u0432\u044b\u0439","type":"select","product_id":"3701","quantity":"1","product_option_value_id":"5663"}] - Syntax errormalformed JSON 
    Can anybody say anything
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Works for me with the example string you've given. Maybe you have a space or something?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    49
    Rep Power
    6
    Hi it's because my $product['option'] is array. Did you use array?
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    No, I used the json strings you printed.

    This:
    Decoding: [{"product_id":"3659","quantity":"2"}] - Syntax error, malformed JSON

    That worked.

    This:
    Decoding: [{"name":"\u041a\u0430\u0440\u0442\u043e\u043d","value":"\u0411\u0435\u043b\u044b\u0439 \u043c\u0430\u0442\u043e\u0432\u044b\u0439","type":"select","product_id":"3701","quantity":"1","prod uct_option_value_id":"5663"}] - Syntax error, malformed JSON

    Also worked.

    Work step by step. I copied the inside of your loop and copied your example json. Both worked fine.

    Code:
    php > $string = '[{"name":"\u041a\u0430\u0440\u0442\u043e\u043d","value":"\u0411\u0435\u043b\u044b\u0439 \u043c\u0430\u0442\u043e\u0432\u044b\u0439","type":"select","product_id":"3701","quantity":"1","product_option_value_id":"5663"}]';
    php > function doString ( $string ) {
    php { echo 'Decoding: ' . $string;                                                                                                                                                                           php {    
    php {     json_decode($string, true); 
    php { 
    php {     switch (json_last_error()) { 
    php {         case JSON_ERROR_NONE: 
    php {             echo ' - No errors'; 
    php {         break; 
    php {         case JSON_ERROR_DEPTH: 
    php {             echo ' - Maximum stack depth exceeded'; 
    php {         break; 
    php {         case JSON_ERROR_STATE_MISMATCH: 
    php {             echo ' - Underflow or the modes mismatch'; 
    php {         break; 
    php {         case JSON_ERROR_CTRL_CHAR: 
    php {             echo ' - Unexpected control character found'; 
    php {         break; 
    php {         case JSON_ERROR_SYNTAX: 
    php {             echo ' - Syntax error, malformed JSON'; 
    php {         break; 
    php {         case JSON_ERROR_UTF8: 
    php {             echo ' - Malformed UTF-8 characters, possibly incorrectly encoded'; 
    php {         break; 
    php {         default: 
    php {             echo ' - Unknown error'; 
    php {         break; 
    php {     } 
    php { 
    php {     echo PHP_EOL; 
    php { }
    php > doString($string);
    Decoding: [{"name":"\u041a\u0430\u0440\u0442\u043e\u043d","value":"\u0411\u0435\u043b\u044b\u0439 \u043c\u0430\u0442\u043e\u0432\u044b\u0439","type":"select","product_id":"3701","quantity":"1","product_option_value_id":"5663"}] - No errors
    php >
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    49
    Rep Power
    6
    I dont know why but it still doesnt work in my browser((
    Instead I've found a way that works for me
    <td class="right"> <input type="checkbox" name="delete[]" value = "<?php echo base64_encode(serialize($product['option'])); ?>" > </td>
    and in php file
    foreach ($data['delete'] as $received) {
    $received = unserialize(base64_decode($received));
    print_r($received);
    }
    Thanks a lot
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    The more pressing question is actually why are you pushing product details down to a form? What's stopping me from opening up this array myself and screwing with the values? You shouldn't be doing this at all.

    Comments on this post

    • ptr2void agrees
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    49
    Rep Power
    6
    This is a long story. I edit site functionality.
  14. #8
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    So you're taking someone else's site and un-breaking it? Go all the way and remove this bit entirely, since it's a massive security hole.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,031
    Rep Power
    377
    i think he means he is doing an "edit product" functionality where admins can change the details.. in that case he needs to get the data and put it in the form elements?
  18. #10
  19. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    If that's the case, the data should not be PHP Serialized in a non-printing form field.

    This is almost certainly doing it wrong.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo