- Posts: 24
- Thank you received: 3
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Getting error while using JSON.parse
- rhg
- Topic Author
- Offline
- Junior Member
Less
More
6 years 1 month ago #165915
by rhg
Getting error while using JSON.parse was created 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
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
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 24
- Thank you received: 3
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"\}');
pt[1]= JSON.parse('\{"AB":"1","BC":"1.309","XY":"0.40"\}');
The topic has been locked.
- DenisChenu
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 13624
- Thank you received: 2490
6 years 1 month ago #165928
by DenisChenu
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.
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
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
- 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.
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
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 13624
- Thank you received: 2490
6 years 3 weeks ago - 6 years 3 weeks ago #166458
by DenisChenu
If you just need to show curly brace to user (for example : make a question about Expression manager) : use html entities
Example
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.
Replied by DenisChenu on topic Getting error while using JSON.parse
? Do you have an example ? Json with or without space are same , javascript with or without space are same too, no ?markusfluer wrote: But some functionality isn't well working with whitespaces, so sometimes, you will need to escape.
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 : &#123;TOKEN:TOKEN&#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.