Layer7 API Management

  • 1.  Escape unallowed characters in JSON

    Broadcom Employee
    Posted Nov 23, 2016 12:16 PM

    Dear community users,

     

    I am currently facing an annoying issue with one of my clients.

     

    I am trying to find a way to cleanly escape unallowed JSON characters in value fields.


    Example of malformed JSON coming from backend (double quotes)


    {
    "key1": "value 1",
    "key2": "valu"e 2"
    }

    correction wanted
    {
    "key1": "value 1",
    "key2": "valu\"e 2"
    }


    I managed to deal with the problem with a regular expression but my solution doesn't work if there are mutiple double quotes in the same value.
    Here is the expression I used : (: ?"[^"]*)("[^"]*)(",?)
    Replacement: $1\\$2$3

     

    Do you have any clean and robust solution to this problem ?

    Thank you very much,



  • 2.  Re: Escape unallowed characters in JSON
    Best Answer

    Broadcom Employee
    Posted Nov 25, 2016 12:31 PM

    Nicolas,

     

    I looked online and found the following article: java - How to escape double quote in the json - Stack Overflow . I tested the suggested regex ([a-zA-Z0-9])(\")([a-zA-Z0-9]) with the replacement $1\\$2$3 you provided. The results from the testing appear to do what you are looking for.

     

    Sincerely,

     

    Stephen Hughes

    Director, CA Support