-1

I have the following string as content in a div. I want to use JSON.parse to convert this into a JSON object. When I try it always fails with Unexpeded token.

    let paramstr=document.querySelector('#cart_payment_params').innerText;
    let params=JSON.parse(paramstr);
    <div id="cart_payment_params" style="display:none;">{
     style: {
      shape: 'pill',
      color: 'blue',
      layout: 'vertical',
      label: 'paypal'
     },
     createOrder: function(data, actions) {
      return actions.order.create({
       purchase_units: [{
        amount: {
         value: '1'
        }
       }]
      });
     },
     onApprove: function(data, actions) {
      return actions.order.capture().then(function(details) {
       alert('Transaction completed by ' + details.payer.name.given_name + '!');
      });
     }
    }</div>

Javascript Error:

Uncaught SyntaxError: Unexpected token u in JSON at position 118
    at JSON.parse (<anonymous>)
Steve Lloyd
  • 773
  • 2
  • 12
  • 33
  • Can you post the JSON you are trying to parse too? – James Burgess Jun 17 '20 at 13:28
  • 7
    Having a function like this is no longer JSON format, so you can not parse it with JSON parsing. JSON supports arrays, objects and primitives types (number, string, boolean, null) only. – robinvrd Jun 17 '20 at 13:28

1 Answers1

0

robinvrd answered this. JSON supports arrays, objects and primitives types (number, string, boolean, null) only.

Steve Lloyd
  • 773
  • 2
  • 12
  • 33