Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Getting error while using JSON.parse

More
7 years 5 months 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
The topic has been locked.
More
7 years 5 months 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 topic has been locked.
More
7 years 5 months 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 support - Plugins, theme and development .
I don't answer to private message.
The following user(s) said Thank You: rhg
The topic has been locked.
  • markusfluer
  • markusfluer's Avatar
  • New Member
  • New Member
More
7 years 5 months ago #166234 by markusfluer
Replied by markusfluer on topic Getting error while using JSON.parse
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 topic has been locked.
More
7 years 5 months ago - 7 years 5 months 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 support - Plugins, theme and development .
I don't answer to private message.
Last edit: 7 years 5 months ago by DenisChenu. Reason: & for &
The following user(s) said Thank You: rhg
The topic has been locked.
Moderators: holchtpartner

Lime-years ahead

Online-surveys for every purse and purpose