Expand my Community achievements bar.

Announcing the launch of new sub-community for Campaign Web UI to cater specifically to the needs of Campaign Web UI users!
SOLVED

JSON response - escaping double quotes

Avatar

Community Advisor

Hi all,

a web service is returning JSON response like this example:  {"name" : "Milan", "tool": "ACM", "reason": "This is a " test"}

You may see the extra double quotes (marked red above) which are causing error. This JSON is properly escaped when is send from server (with one backslash) but after is received in ACM it is lost, therefore I need another one for JavaScript.

I tried str.replace function but this escape all double quotes which is not suitable for me. Also, I tried some things with JSON.stringify() and JSON.parse() but without success.

Do you have any idea how to resolve this?

Thanks,

Milan

1 Accepted Solution

Avatar

Correct answer by
Level 10

Okay then I think there's something wrong with your API.

I think what is going on is that your API:

  1. Accepts JSON correctly.
  2. Stores the object correctly.
  3. Returns the JSON wrong.

I have the feeling your API might accept application/json but returns application/text or something like that, can you check?

Basically what should happen when you create a resource is that your API should return the same thing you sent to it, plus the ID of the created resource. For example:

Create a resource

POST /address

{"street": "this is a \" test"}

>>> {"id": "1", "street": "this is a \" test"}

Fetch a resource

GET /address/1

>>> {"id": "1", "street": "this is a \" test"}

Database content

id     street

-----  ----------------

1             this is a " test

View solution in original post

7 Replies

Avatar

Community Advisor

Hi,

Could you provide exact examples of what you're trying to accomplish?

E.g. Input string is 'x"x', JSON.parse() is returning 'x"x', expected output is 'x\"x',.

Thanks,

-Jon

Avatar

Community Advisor

Hi Jon,

I want to assign below values from received JSON to variables:

            "address":{

              "houseNumber":"20",

              "street":"Massima d"Agnolia",

              "zip":"10000",

              "city":"" }

var house_number: 20    - this is fine

var street:  Massima d    - this is wrong becasue " was not escaped, "Agnolia" is missing

var zip:                            -  this will be undefined, can't find this value becase next one is "Agnolia" instead of "zip"

I need a way to properly escape JSON from JavaScript in order to continue processing data.

From this JSON example value "Massima d"Agnolia"  needs to be converted to:  "Massima d\"Agnolia"

Thanks,

Milan

Avatar

Level 10

Hi Milan,

Unfortunately, trying to correct the JSON string before parsing is going to be incredibly complicated. I'm saying this because the field in question (street) seems to be free-text, so it will be basically impossible to identify what is JSON syntax and what is field content.

Imagine I entered the following value for the street field:

Fake Street", "fakeProperty": "fake

This would result in the following JSON (expected JSON syntax in blue, value in underlined red)

"street: "Fake Street", "fakeProperty": "fake"

As you can see, this is actually valid JSON! I don't know where your data comes from, but if it comes from an input form for example, then anybody could manipulate your JSON (and resulting JS objects) by entering such values in the input form fields, and you would never be able to detect it as "invalid" (at least not in a generic way).

For that reason I don't think that finding a JS hack to solve to fix the JSON string is the right way to solve the issue. Instead, i think you should attempt to fix the method of fetching the JSON so that it maintains its correctly escaped format. Could you tell us how you're doing that?

If that is not possible for whatever reason, then try to avoid double-quotes in the data by adding stricter validation on input and doing a one-shot fix for all existing data.

Avatar

Community Advisor

Hi theop76211228,

actually I am sending this JSON to an API and value for the street field escaping with two backslash (one for ACM JavaScript and second for JSON which enables to transfer and store this value properly in original state). After API process the data, JSON is returned with the same address (as sent one) properly escaped but with just one backslash, therefore problem is coming up in JavaScript (I need one more backslash). I am unable to change business process, to force API to add one more backslash just for me and so on... I am trying to find solution on my side (ACM) and I am aware that is difficult, but it is just impossible that no one had this problem earlier

Example:

var jsonResponseFromAPI = getJSONResponseFromAPI("retrieveFunction",jsonId,credentials,null);

     part["address_street"] = jsonResponseFromAPI.entry[count].input.address.street;

     part["zip"] = jsonResponseFromAPI.entry[count].input.address.zip;

So in this code, "zip" will be undefined because previous (street) field will be cut at " character which is not escaped twice.

On of the quick solutions could be to initial JSON escape with three backslash before sending, so one will be returned (the missing one), but this is just crazy and address will be stored in API with character "\" which is wrong.

Regards,

Milan

Avatar

Level 10

Hi Milan,

Could you just confirm that I have this right? Your process is basically this:

1. You send an object to the API (including the street field)

2. The API saves this object in some database

3. You fetch the object in ACM using your getJSONResponseFromAPI() function.

It would look like this correct?

If I've got this wrong, could you amend the diagram? You can find the code here and the editor here.

Also, could you post your getJSONResponseFromAPI() function?

Avatar

Community Advisor

HI,

yes, this is general process. Below you may find important parts of the mentioned function:

function getJSONResponseFromAPI(method,,credentials,some_parameters){

  var jsonResponseFromAPI = null;

 

  try{ 

    var endPoint = 'http://dump_address.com'

    var apiCallMethod = "GET";

    var http = new HttpClientRequest(endPoint);

    var buffer = new MemoryBuffer();

   

    ....

   

    http.execute();

   

    var rsp = http.response;

    var responseContent =  rsp.body.toString(rsp.codePage);   

    buffer.dispose();

     ...

    jsonResponseFromAPI = JSON.parse(responseContent);

     ...

  return jsonResponseFromAPI;

}

Avatar

Correct answer by
Level 10

Okay then I think there's something wrong with your API.

I think what is going on is that your API:

  1. Accepts JSON correctly.
  2. Stores the object correctly.
  3. Returns the JSON wrong.

I have the feeling your API might accept application/json but returns application/text or something like that, can you check?

Basically what should happen when you create a resource is that your API should return the same thing you sent to it, plus the ID of the created resource. For example:

Create a resource

POST /address

{"street": "this is a \" test"}

>>> {"id": "1", "street": "this is a \" test"}

Fetch a resource

GET /address/1

>>> {"id": "1", "street": "this is a \" test"}

Database content

id     street

-----  ----------------

1             this is a " test