Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Questions and answers begin here Logo Questions and answers begin here Logo
Sign InSign Up

Questions and answers begin here

Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Blog
  • Contact Us

How to solve JSON.parse: bad control character in string literal in this code?

Home/ Questions/Q 278
Closed
How to solve JSON.parse: bad control character in string literal in this code?
jo.pixels
jo.pixels Teacher

I have been trying to parse a JSON string, but I keep getting an error message that says “JSON.parse: bad control character in string literal”. I have tried different approaches, but none of them seem to work. The code that I’m using is below:

const json = '{"result":true, "count":42}';
const obj = JSON.parse(json);
console.log(obj.count);

I am not sure what I am doing wrong or what the error message means. I have looked online for help, but I haven’t found a solution that works for me. Can someone please help me understand what is causing the error and how to fix it?
I have also tried using the escape character “” before the special characters in the JSON string

control-characterserror-handlingjsonparsingstring-literal
  • 1
  • 0 Followers
  • 1
  • Report

Sorry this question is closed.

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    alex.papantoniou1 Pundit
    2022-02-20T20:37:11+00:00Added an answer about 1 year ago

    Hey there!
    I understand that you are having trouble with the “JSON.parse: bad control character in string literal” error message when working on your code. This error is caused by a control character that cannot be parsed by the JSON.parse() method. Control characters are special characters that cannot be rendered like regular text, such as line breaks or tabs, and can change the behavior of certain functions.
    One way to solve this issue is by using a regular expression to remove any control characters from your string before parsing it with JSON.parse(). You can use the replace() method to replace the control characters with an empty string, like this:
    “`
    var jsonString = ‘your JSON string with control characters’;
    jsonString = jsonString.replace(/[u0000-u001F]+/g, ”); // remove control characters
    var jsonObject = JSON.parse(jsonString);
    “`
    The regular expression `[u0000-u001F]` matches any control character in the string, and the `+` sign and `g` flag ensure that all occurrences are replaced. After removing the control characters, the JSON.parse() method should be able to parse the string without any issues.
    If you want to prevent control characters from being included in your JSON data in the first place, you should escape them properly. Use escape sequences for special characters or use a library like lodash or jQuery to handle JSON serialization.
    In conclusion, the “JSON.parse: bad control character in string literal” error usually occurs when a control character is present in the JSON data string. You can remove the control characters using a regular expression to fix the issue, or escape them properly to prevent them from being included in the JSON data.

    • 27
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. santos.andersonferreira Begginer
    2022-02-28T20:44:36+00:00Added an answer about 1 year ago

    One possible solution for this issue would be to use the `JSON.parse()` method. This method allows you to convert a JSON string to a JavaScript object. You can then access the object’s properties using dot notation. In your specific case, you could try the following code:
    “`
    var obj = JSON.parse(data.replace(/[u0000-u0019]+/g,””));
    “`

    Here, `data` is the JSON string you want to parse, and the `replace()` method is used to remove any control characters from the string. After that, the `JSON.parse()` method is called on the modified string.

    It’s important to note that using `JSON.parse()` on untrusted JSON data can be a security risk, as it may allow an attacker to execute arbitrary code on your page. To mitigate this risk, you can use a library like `DOMPurify` to sanitize the JSON data before parsing it.

    I hope this helps! Let me know if you have any questions or concerns.

    • 18
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report