Welcome to the LimeSurvey Community Forum

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

Getting error while using JSON.parse

  • rhg
  • rhg's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
6 years 1 month 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.
  • rhg
  • rhg's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
6 years 1 month ago #165917 by rhg
Replied by rhg on topic Getting error while using JSON.parse
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.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 1 month ago #165928 by DenisChenu
Replied by DenisChenu on topic Getting error while using JSON.parse
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 , plugin 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
  • Visitor
  • Visitor
6 years 3 weeks 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.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 3 weeks ago - 6 years 3 weeks ago #166458 by DenisChenu
Replied by DenisChenu on topic Getting error while using JSON.parse

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 , plugin development .
I don't answer to private message.
Last edit: 6 years 3 weeks ago by DenisChenu. Reason: & for &
The following user(s) said Thank You: rhg
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose