Share via

A problem occurs when using regular expressions to extract only the json part of the string-format code written in the SharePoint script tag and parsing it with JSON.parse().

2023-12-27T04:35:04.9433333+00:00

A problem occurs when using regular expressions to extract only the json part of the string-format code written in the SharePoint script tag and parsing it with JSON.parse().

When I process in JavaScript, I get an error. However, when I paste the same string into the console and use JSON.parse(), it converts to JSON without any issues.

I’ve tried various things, suspecting that the character encoding or special characters might be causing the issue, but I haven’t been able to resolve it.

If you are familiar with the specifications of SharePoint, could you please explain why this might be happening?

Extracted sample_data json data

{"user" : {"@odata.context":

Success (Devtools)

const data = '{"user" : {"@odata.context":.....}'
const jsondata = JSON.parse(sample_data);
Console.log(jsondata);

-------> success

False (javascript)

const jsondata = JSON.parse(sample_data);
console.dir(jsonData);

-----> error
Uncaught SyntaxError: Expected ',' or '}' after property value in JSON at position 965 ~~~~
Microsoft 365 and Office | Development | Office JavaScript API
Microsoft 365 and Office | SharePoint | Development
Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,496 Reputation points Microsoft External Staff
    2023-12-27T08:00:53.54+00:00

    Hi @Ryouta Kawasaki (川﨑 涼太),

    The sample data you are using is a invaild json format. The Devtools might have automatic error correction mechanism. For the JSON.parse in js, you might take following steps to fix the issue. Please refer to the nice article

    https://medium.com/@codewithmebalaji/how-to-parse-incorrect-or-partial-json-to-valid-json-in-javascript-5f71eee1b227


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.