Getting error while using JSON.parse

More
10 months 3 weeks ago #165915 by rhg
In one my script tag, I am having
pt=[]
pt[1]= JSON.parse('{"AB":"1","BC":"1.309","XY":"0.40"}');


But, when I am previewing question, I am getting Uncaught SyntaxError: missing ) after argument list. When I click error on console, it shows pt[1]= JSON.parse('<span class='em-expression em-haveerror'><span title='' class='em-var-string'>"AB"</span><span title='Unsupported syntax' class='em-error'> : </span><span title='' class='em-var-string'>"1"</span>, <span title='' class='em-var-string'>"BC"</span><span title='Unsupported syntax' class='em-error'> : </span><span title='' class='em-var-string'>"1.309"</span>, <span title='' class='em-var-string'>"XY"</span><span title='Unsupported syntax' class='em-error'> : </span><span title='' class='em-var-string'>"0.40"</span></span>');

I checked the same code works in javascript console. What could be the problem. How I can use JSON objects in limesurvey script tag

Please Log in or Create an account to join the conversation.

More
10 months 3 weeks ago #165917 by rhg
I have to escape curly braces in order to use JSON.parse.

pt[1]= JSON.parse('\{"AB":"1","BC":"1.309","XY":"0.40"\}');
The following user(s) said Thank You: markusfluer

Please Log in or Create an account to join the conversation.

More
10 months 3 weeks ago #165928 by DenisChenu
If you use {something} inside a question : Expression Manager treat it.

To disable : just add a space : { something }

No need to escape, but escaping disable too Expression Manager

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .
The following user(s) said Thank You: rhg

Please Log in or Create an account to join the conversation.

More
10 months 2 weeks ago #166234 by markusfluer
That is correct, you have to escape the curly braces.
Alternatively, what shnoulle wrote is also correct, a simple whitespace will also prevent the EM from parsing it.
But some functionality isn't well working with whitespaces, so sometimes, you will need to escape.
The following user(s) said Thank You: LouisGac, rhg

Please Log in or Create an account to join the conversation.

More
10 months 2 weeks ago - 10 months 2 weeks ago #166458 by DenisChenu

markusfluer wrote: But some functionality isn't well working with whitespaces, so sometimes, you will need to escape.

? Do you have an example ? Json with or without space are same ,JavaScript with or without space are same too, no ?

If you just need to show curly brace to user (for example : make a question about Expression Manager) : use html entities

Example

Did this code is valid in all surveys : &amp;#123;TOKEN:TOKEN&amp;#125;


Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .
Last edit: 10 months 2 weeks ago by DenisChenu. Reason: & for &
The following user(s) said Thank You: rhg

Please Log in or Create an account to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!