# Xojo badly escape an apostrophe in a json item (for websdkcontrol ?)

**URL:** <https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233>\
**Category:** Web SDK\
**Created:** [October 24, 2021, 2:23pm UTC](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233 "2021-10-24T14:23:01Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jean-Yves\_Pochez](https://forum.xojo.com/user_avatar/forum.xojo.com/jean-yves_pochez/32/21769_2.png) [@Jean-Yves\_Pochez](https://forum.xojo.com/u/Jean-Yves_Pochez)\
**Post date:** [October 24, 2021, 2:23pm UTC](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233/1 "2021-10-24T14:23:01Z")

</div>

stuck with that bug today.

if you add some string containing an apostrophe in a json like “it’s me”  
xojo doesn’t add an escape char \ before it  
if you add the escape yourself, “it’s me” then xojo adds two \ !!!

![Capture d’écran 2021-10-24 à 16.16.47](https://forum.xojo.com/uploads/default/original/2X/f/f1f20c49c2697cd914f5177be11aa409e10a97d8.png)  
 ![Capture d’écran 2021-10-24 à 16.17.02](https://forum.xojo.com/uploads/default/original/2X/e/e8db7929941e4da6537c4b4026d39a1e21623378.png)

\<[https://xojo.com/issue/66306](https://xojo.com/issue/66306)\>

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [October 24, 2021, 2:24pm UTC](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233/2 "2021-10-24T14:24:22Z")

</div>

Why do you consider this a bug? Single quotes do not need to be escaped according to the standard.

---

<div class="post-metadata">

**Author:** ![Jean-Yves\_Pochez](https://forum.xojo.com/user_avatar/forum.xojo.com/jean-yves_pochez/32/21769_2.png) [@Jean-Yves\_Pochez](https://forum.xojo.com/u/Jean-Yves_Pochez)\
**Post date:** [October 24, 2021, 2:37pm UTC](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233/3 "2021-10-24T14:37:34Z")

</div>

because I send this JSON to a websdkcontrol, and the parsejson method in the javascript environment does not recognize it. the websdkcontrol gets destroyed because of a bad syntax.

so may be it’s only a websdk related problem ? if you assume a single quote doesn’t need to be escaped ?  
I’ve seen some posts (on stackexchange) where people wanted to escape single quotes for javascript usage…

may be @Greg_O_Lone can explain more to us ?

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [October 24, 2021, 3:03pm UTC](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233/4 "2021-10-24T15:03:41Z")

</div>

> [@Jean-Yves\_Pochez](#):
>
> I’ve seen some posts (on stackexchange) where people wanted to escape single quotes for javascript usage

Only necessary when your string is surround by single quotes needing such escaping. Below the 3 vars leads to the same JSON value.

```auto
// Javascript:
j1 = ["it's me"];
j2 = eval('["it\'s me"]');
j3 = eval("[\"it's me\"]");

```

---

<div class="post-metadata">

**Author:** ![Jean-Yves\_Pochez](https://forum.xojo.com/user_avatar/forum.xojo.com/jean-yves_pochez/32/21769_2.png) [@Jean-Yves\_Pochez](https://forum.xojo.com/u/Jean-Yves_Pochez)\
**Post date:** [October 24, 2021, 3:19pm UTC](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233/5 "2021-10-24T15:19:22Z")

</div>

in my actual problem, I make a json object as a string, containing a single quote ’  
`js.value("dbvalue") = "it's me"`  
this is sent to the browser side of the websdkcontrol, and parsed

```auto
super.updateControl(data);
let js = $.parseJSON(data);
this.mDataJSon = js.dbValue;

```

the js.dbValue is badly formed or empty. this leads to a javascript error on the browser.

how can I workaround this ?

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [October 24, 2021, 4:02pm UTC](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233/6 "2021-10-24T16:02:16Z")

</div>

> [@Jean-Yves\_Pochez](#):
>
> js.value(“dbvalue”) = “it’s me”

Let’s wait someone used to use the “websdk” to chime in, but now your example differs from the first case, a

`js.value("dbvalue") = "it's me"`

Should render a JSON like

`{"dbvalue": "it's me"}`

and not an array as previously reported.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [October 24, 2021, 4:10pm UTC](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233/7 "2021-10-24T16:10:20Z")

</div>

> [@Jean-Yves\_Pochez](#):
>
> ```auto
> js.value("dbvalue") = "it's me"
> this is sent to the browser side of the websdkcontrol, and parsed
> ...
> this.mDataJSon = js.dbValue;
> 
> ```

Javascript is a case sensitive language, maybe you are missing it here?  
` this.mDataJSon = js.dbvalue;`

---

<div class="post-metadata">

**Author:** ![Jean-Yves\_Pochez](https://forum.xojo.com/user_avatar/forum.xojo.com/jean-yves_pochez/32/21769_2.png) [@Jean-Yves\_Pochez](https://forum.xojo.com/u/Jean-Yves_Pochez)\
**Post date:** [October 24, 2021, 4:14pm UTC](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233/8 "2021-10-24T16:14:30Z")

</div>

> [@Rick\_Araujo](#):
>
> Javascript is a case sensitive language, maybe you are missing it here?  
> ` this.mDataJSon = js.dbvalue;`

thanks I already checked that. it’s a typing failure only. the case is the same between xojo and the javascript

---

<div class="post-metadata">

**Author:** ![Jean-Yves\_Pochez](https://forum.xojo.com/user_avatar/forum.xojo.com/jean-yves_pochez/32/21769_2.png) [@Jean-Yves\_Pochez](https://forum.xojo.com/u/Jean-Yves_Pochez)\
**Post date:** [October 24, 2021, 4:17pm UTC](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233/9 "2021-10-24T16:17:47Z")

</div>

> [@Rick\_Araujo](#):
>
> but now your example differs from the first case, a  
> `js.value("dbvalue") = "it's me"`  
> Should render a JSON like  
> `{"dbvalue": "it's me"}`  
> and not an array as previously reported.

sorry for the mix, but it’s the same with an array or a dictionnary  
 ![Capture d’écran 2021-10-24 à 18.16.42](https://forum.xojo.com/uploads/default/original/2X/0/0ff69e9b51ee9e47808a55c4fd03e86004e1c6b8.png)

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [October 24, 2021, 4:19pm UTC](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233/10 "2021-10-24T16:19:40Z")

</div>

This is correct, without Xojo adding the extra backslash you will lose it when JS renders the value:

> [@Jean-Yves\_Pochez](#):
>
> ![Capture d’écran 2021-10-24 à 18.16.42](https://forum.xojo.com/uploads/default/original/2X/0/0ff69e9b51ee9e47808a55c4fd03e86004e1c6b8.png)

---

<div class="post-metadata">

**Author:** ![Jean-Yves\_Pochez](https://forum.xojo.com/user_avatar/forum.xojo.com/jean-yves_pochez/32/21769_2.png) [@Jean-Yves\_Pochez](https://forum.xojo.com/u/Jean-Yves_Pochez)\
**Post date:** [October 24, 2021, 4:26pm UTC](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233/11 "2021-10-24T16:26:45Z")

</div>

so I just replaced any single quote “’” with “\u0027” and … it works !

but it should be correctly handled ?

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [October 24, 2021, 5:23pm UTC](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233/12 "2021-10-24T17:23:42Z")

</div>

If you can, add a proof of the problem to your case.

I would like to see what you see because I still don’t understand your problem.

---

<div class="post-metadata">

**Author:** ![Jean-Yves\_Pochez](https://forum.xojo.com/user_avatar/forum.xojo.com/jean-yves_pochez/32/21769_2.png) [@Jean-Yves\_Pochez](https://forum.xojo.com/u/Jean-Yves_Pochez)\
**Post date:** [October 24, 2021, 5:39pm UTC](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233/13 "2021-10-24T17:39:03Z")

</div>

well, to sum up :  
in a websdkuicontrol class, in the serialize event, you need to build a jsonitem to send to the browser.  
if like me you want to send strings containing single quotes, you’re stuck  
if you send it raw, it does not get decoded in the parseJson method in the browser side  
if you escape it “\’”, xojo adds another \ and it gets “\\’” to the bowser which is not decoded again.

the solution was for me to replace it with the “\u0027” and it is correctly decoded in the browser parseJson.

you have to escape the double quote " to send it into a json, I thought it would be the same for single quote, but as Kem said earlier, in the json standard you don’t need to escape single quotes.  
so what is the best option ?

anyway I hope some future reader looking for a workaround will come here.

---

<div class="post-metadata">

**Author:** ![Aurelian\_N](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/b4bc9f/32.png) [@Aurelian\_N](https://forum.xojo.com/u/Aurelian_N)\
**Post date:** [October 24, 2021, 11:56pm UTC](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233/14 "2021-10-24T23:56:23Z")

</div>

no idea if it helps but did you try encoding your data that goes to the web socket ? if in case you are using that ? so far on Xojo side is known on loosing encodings here and there and the best way to do that is to define encodings when sending data over, I encountered that a lot with special characters and same as your case . Indeed " ’ " should always work in this case but you never know.

Hope it helps.

---

<div class="post-metadata">

**Author:** ![Jean-Yves\_Pochez](https://forum.xojo.com/user_avatar/forum.xojo.com/jean-yves_pochez/32/21769_2.png) [@Jean-Yves\_Pochez](https://forum.xojo.com/u/Jean-Yves_Pochez)\
**Post date:** [October 25, 2021, 6:18am UTC](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233/15 "2021-10-25T06:18:32Z")

</div>

yes I always send utf8 encoded strings in the serialize event. good practice with websdk !

---

<div class="post-metadata">

**Author:** ![Attilio\_Punzi](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@Attilio\_Punzi](https://forum.xojo.com/u/Attilio_Punzi)\
**Post date:** [October 25, 2021, 6:50am UTC](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233/16 "2021-10-25T06:50:21Z")

</div>

In my opinion the best way to pass data to websdk is to use base64 encoding.

In Xojo you use:  
var t as String = “string in utf8”  
js.value(“stringVal”) = EncodeBase64(t,1)

In JS:  
let js = $.parseJSON(data);  
let str = XojoWeb.DecodeBase64(js.stringVal);

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [October 25, 2021, 12:09pm UTC](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233/17 "2021-10-25T12:09:39Z")

</div>

Case closed as “not a bug”. They say you are doing some kind of misuse of the framework.

---

<div class="post-metadata">

**Author:** ![Jean-Yves\_Pochez](https://forum.xojo.com/user_avatar/forum.xojo.com/jean-yves_pochez/32/21769_2.png) [@Jean-Yves\_Pochez](https://forum.xojo.com/u/Jean-Yves_Pochez)\
**Post date:** [October 25, 2021, 12:25pm UTC](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233/18 "2021-10-25T12:25:21Z")

</div>

I did not understand it that way. only what Kem said : escaping single quote is not required.

but that there may be a bug with only the websdk, I have to make another bug report with another test project for web.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [October 25, 2021, 12:56pm UTC](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233/19 "2021-10-25T12:56:56Z")

</div>

> [@Jean-Yves\_Pochez](#):
>
> I have to make another bug report with another test project for web.

👍🏻 Yep. A simple test to show and prove your point. If it gets the status of a confirmed bug, it must be fixed, if not, you may get an explanation about what’s wrong in the process being done and the best way of doing it.

---

<div class="post-metadata">

**Author:** ![Jean-Yves\_Pochez](https://forum.xojo.com/user_avatar/forum.xojo.com/jean-yves_pochez/32/21769_2.png) [@Jean-Yves\_Pochez](https://forum.xojo.com/u/Jean-Yves_Pochez)\
**Post date:** [October 25, 2021, 1:20pm UTC](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233/20 "2021-10-25T13:20:23Z")

</div>

my workaround with \u0027 is working so fine that I may not take the time to make a small test project !

[Next page](https://forum.xojo.com/t/xojo-badly-escape-an-apostrophe-in-a-json-item-for-websdkcontrol/66233.md?page=2)
