skip to Main Content

I had a php object..that I converted to json text using json_encode()
function. Then I passed it on $.parseJSON() function on javascript.

Note:The description field in json object contains text encoded in html entities..

On execution $.parseJSON() function says…

SyntaxError: JSON.parse: bad control character in string literal at
line 1 column 133 of the JSON data

However if description field just just had normal text like “description”:”hy” there would be no problem in parsing $.parseJSON() function..

The need is I have to store description field with html entities that I have on database..

Help me out Guys…

$(window).load(function editPage(){
         var page_data = $.parseJSON('{"id":"1","title":"FAQ","summary":"Frequently Asked Questions are available here","description":"<!DOCTYPE html>rn<html>rn<head>rn</head>rn<body>rn<h6 class="section-heading__heading heading--1">What does Shopify do?</h6>rn<p> </p>rn<div class="grid-item grid-6 grid-push-1">rn<div class="long-form-content ">rn<p>Shopify is a complete <code><a class="body-link" href="https://www.shopify.com/" target="_blank" rel="noopener">ecommrce solution</a></code> that allows you to set up an online store to sell your goods. It lets you organize your products, customize your storefront, accept credit card payments, track and respond to orders — all with a few clicks of the mouse.</p>rn</div>rn</div>rn</body>rn</html>","status":"1","featured_image":"Page-1519912947103.jpg","added_date":"2018-03-01 19:46:28","updated_date":"2018-03-02 10:13:11"}');
               console.log(page_data);
               }
            );

2

Answers


  1. Chosen as BEST ANSWER

    After various suggestion by helpful hands ..my now working code is..

    <script>
             $(window).load(function editPage(){
    
              var jsonString= '{"id":"1","title":"FAQ","summary":"Frequently Asked Questions are available here","description":"&lt;!DOCTYPE html&gt;rn&lt;html&gt;rn&lt;head&gt;rn&lt;/head&gt;rn&lt;body&gt;rn&lt;h6 class=&quot;section-heading__heading heading--1&quot;&gt;What does Shopify do?&lt;/h6&gt;rn&lt;p&gt;&amp;nbsp;&lt;/p&gt;rn&lt;div class=&quot;grid-item grid-6 grid-push-1&quot;&gt;rn&lt;div class=&quot;long-form-content &quot;&gt;rn&lt;p&gt;Shopify is a complete &lt;code&gt;&lt;a class=&quot;body-link&quot; href=&quot;https://www.shopify.com/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;ecommrce solution&lt;/a&gt;&lt;/code&gt; that allows you to set up an online store to sell your goods. It lets you organize your products, customize your storefront, accept credit card payments, track and respond to orders &amp;mdash; all with a few clicks of the mouse.&lt;/p&gt;rn&lt;/div&gt;rn&lt;/div&gt;rn&lt;/body&gt;rn&lt;/html&gt;","status":"1","featured_image":"Page-1519912947103.jpg","added_date":"2018-03-01 19:46:28","updated_date":"2018-03-02 10:13:11"}';
              jsonString=jsonString.replace(/rn/g, '\r\n');
              var page_data =$.parseJSON(jsonString);
              console.log(page_data);
               }
            );
           </script>
    

  2. This is because JSON.parse cannot parse some special characters that are n, t, r and f.You need to replace it before parsing.

    $(window).load(function editPage(){
            var jsonString='{"id":"1","title":"FAQ","summary":"Frequently Asked Questions are available here","description":"&lt;!DOCTYPE html&gt;rn&lt;html&gt;rn&lt;head&gt;rn&lt;/head&gt;rn&lt;body&gt;rn&lt;h6 class=&quot;section-heading__heading heading--1&quot;&gt;What does Shopify do?&lt;/h6&gt;rn&lt;p&gt;&amp;nbsp;&lt;/p&gt;rn&lt;div class=&quot;grid-item grid-6 grid-push-1&quot;&gt;rn&lt;div class=&quot;long-form-content &quot;&gt;rn&lt;p&gt;Shopify is a complete &lt;code&gt;&lt;a class=&quot;body-link&quot; href=&quot;https://www.shopify.com/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;ecommrce solution&lt;/a&gt;&lt;/code&gt; that allows you to set up an online store to sell your goods. It lets you organize your products, customize your storefront, accept credit card payments, track and respond to orders &amp;mdash; all with a few clicks of the mouse.&lt;/p&gt;rn&lt;/div&gt;rn&lt;/div&gt;rn&lt;/body&gt;rn&lt;/html&gt;","status":"1","featured_image":"Page-1519912947103.jpg","added_date":"2018-03-01 19:46:28","updated_date":"2018-03-02 10:13:11"}';
            jsonString=jsonString.replace(/n/g, "\n")
                .replace(/r/g, "\r")
                .replace(/t/g, "\t")
                .replace(/f/g, "\f");
                 var page_data = $.parseJSON(jsonString);
                       console.log(page_data);
                       }
                    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search