How to escape JSON objects in webhook?

  • Profile Image
    ivancace
    Asked on November 15, 2013 at 08:33 AM

    We are using webhook, but we have a problem when a there is doble quote, Can you scape this character in the json you send by webhook?

     

    thanks

  • Profile Image
    ardy0689
    Answered on November 15, 2013 at 10:31 AM

    Hello, thanks for posting your inquiry here on the forum.

    There are a few ways that you can escape special characters depending on your goals.

    Normally this function escapes the values in the json object:

    http://pastiebin.com/52863dc6d218f

     

    Normal escaping for string manipulations:

    Outputs "Full Name"

    echo "\"Full Name\"";

    However, if you are going to use a string and insert it on a database, it is important that you use this instead:

    Assuming $string contains a value with "Full name" with double quotes literally.

    $string = mysqli_real_escape_string($string);

     

    These are all in PHP script. If you are using a different server script, let us know so that we can assist you on escaping characters.

    If you need further assistance, please do not hesitate to ask. Thank you

  • Profile Image
    ivancace
    Answered on November 16, 2013 at 04:04 AM

    We are using Java, Gson framework. And is imposible for that framework parse values with double quote.

    http://code.google.com/p/google-gson/source/browse/tags/gson-2.2.1/src/test/java/com/google/gson/functional/EscapingTest.java

  • Profile Image
    NeilVicente
    Answered on November 16, 2013 at 08:25 AM

    @ivancace

    Try using the Java equivalent of the following PHP codes before passing the data to Gson:

    function unescapeJSON($str){
                    return str_replace("\\'","'" ,str_replace('\\"', '"', $str));
    }

    $newData = unescapeJSON($rawData);


    The goal is to simply replace \\' and \\" with ' and " , respectively.

  • Profile Image
    ivancace
    Answered on November 16, 2013 at 09:18 AM

    thanks solve!!