json.parse when values may contain rich text

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

json.parse when values may contain rich text

zcurtis
This post was updated on .
I have data from a trusted source where values may contain html (from rich text fields). However, the values may not always be properly formatted (eg, no closing tag). The data is json formatted and then output on the page within html tags and then 'read in' to be parsed. In the simplified example below I get the following error on 'data2':

SyntaxError: Unexpected token <

So, 'data1' parses ok, but 'data2' does not.

<span id="data1">[{"test":"xy<b>z</b>"}]</span>
<span id="data2">[{"test":"xy<b>z"}]</span>
<script>
require(['dojo/dom','dojo/json','dojo/domReady!'], 
  function(dom,json) {
    var data1 = json.parse(dom.byId('data1').innerHTML);
    var data2 = json.parse(dom.byId('data2').innerHTML);
  });
</script>

I am drawing a blank on how to proceed as we do want to the rich text to render as html and not as text and I wouldn't know how to account for the improperly formatted html when those cases do occur and cause the parse error. Suggestions?
Reply | Threaded
Open this post in threaded view
|

Re: json.parse when values may contain rich text

Karl Tiedt
HTMLEncode your JSON (Or the values before converting to JSON?)

-Karl Tiedt

On Fri, Mar 11, 2016 at 2:46 PM, zcurtis <[hidden email]> wrote:
I have data from a trusted source where values may contain html (from rich
text fields). However, the values may not always be properly formatted (eg,
no closing tag). The data is json formatted and then output on the page
within html tags and then 'read in' to be parsed. In the simplified example
below I get the following error on 'data2':

SyntaxError: Unexpected token <

So, 'data1' parses ok, but 'data2' does not.

[{"test":"xy*z*"}]
[{"test":"xy*z"}]


I am drawing a blank on how to proceed as we do want to the rich text to
render as html and not as text and I wouldn't know how to account for the
improperly formatted html when those cases do occur and cause the parse
error. Suggestions?




--
View this message in context: http://dojo-toolkit.33424.n3.nabble.com/json-parse-when-values-may-contain-rich-text-tp4006777.html
Sent from the Dojo Toolkit mailing list archive at Nabble.com.
--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest


--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest