Syntax error json parse error

The JavaScript exceptions thrown by JSON.parse() occur when string failed to be parsed as JSON.

The JavaScript exceptions thrown by JSON.parse() occur when string failed
to be parsed as JSON.

Message

SyntaxError: JSON.parse: unterminated string literal
SyntaxError: JSON.parse: bad control character in string literal
SyntaxError: JSON.parse: bad character in string literal
SyntaxError: JSON.parse: bad Unicode escape
SyntaxError: JSON.parse: bad escape character
SyntaxError: JSON.parse: unterminated string
SyntaxError: JSON.parse: no number after minus sign
SyntaxError: JSON.parse: unexpected non-digit
SyntaxError: JSON.parse: missing digits after decimal point
SyntaxError: JSON.parse: unterminated fractional number
SyntaxError: JSON.parse: missing digits after exponent indicator
SyntaxError: JSON.parse: missing digits after exponent sign
SyntaxError: JSON.parse: exponent part is missing a number
SyntaxError: JSON.parse: unexpected end of data
SyntaxError: JSON.parse: unexpected keyword
SyntaxError: JSON.parse: unexpected character
SyntaxError: JSON.parse: end of data while reading object contents
SyntaxError: JSON.parse: expected property name or '}'
SyntaxError: JSON.parse: end of data when ',' or ']' was expected
SyntaxError: JSON.parse: expected ',' or ']' after array element
SyntaxError: JSON.parse: end of data when property name was expected
SyntaxError: JSON.parse: expected double-quoted property name
SyntaxError: JSON.parse: end of data after property name when ':' was expected
SyntaxError: JSON.parse: expected ':' after property name in object
SyntaxError: JSON.parse: end of data after property value in object
SyntaxError: JSON.parse: expected ',' or '}' after property value in object
SyntaxError: JSON.parse: expected ',' or '}' after property-value pair in object literal
SyntaxError: JSON.parse: property names must be double-quoted strings
SyntaxError: JSON.parse: expected property name or '}'
SyntaxError: JSON.parse: unexpected character
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data

Error type

What went wrong?

JSON.parse() parses a string as JSON. This string has to be valid JSON
and will throw this error if incorrect syntax was encountered.

Examples

JSON.parse() does not allow trailing commas

Both lines will throw a SyntaxError:

JSON.parse("[1, 2, 3, 4,]");
JSON.parse('{"foo": 1,}');
// SyntaxError JSON.parse: unexpected character
// at line 1 column 14 of the JSON data

Omit the trailing commas to parse the JSON correctly:

JSON.parse("[1, 2, 3, 4]");
JSON.parse('{"foo": 1}');

Property names must be double-quoted strings

You cannot use single-quotes around properties, like ‘foo’.

JSON.parse("{'foo': 1}");
// SyntaxError: JSON.parse: expected property name or '}'
// at line 1 column 2 of the JSON data

Instead write «foo»:

JSON.parse('{"foo": 1}');

Leading zeros and decimal points

You cannot use leading zeros, like 01, and decimal points must be followed by at least
one digit.

JSON.parse('{"foo": 01}');
// SyntaxError: JSON.parse: expected ',' or '}' after property value
// in object at line 1 column 2 of the JSON data

JSON.parse('{"foo": 1.}');
// SyntaxError: JSON.parse: unterminated fractional number
// at line 1 column 2 of the JSON data

Instead write just 1 without a zero and use at least one digit after a decimal point:

JSON.parse('{"foo": 1}');
JSON.parse('{"foo": 1.0}');

See also

Deeksha Agarwal

Posted On: April 5, 2018

view count26739 Views

Read time3 Min Read

JSON or JavaScript Object Notation is a ubiquitous data format used by all sorts of mobile and web apps for asynchronous browser-server communication. JSON is an extremely popular data format, very easy to work with, compatible with every major programming language, and is supported by every major browser. However just like any programming language, it throws a lot of errors when it decide that today is not going to be your day.

JSON.Parse Syntax Errors

In most web applications, nearly all data transferred from web server is transmitted in a string format. To convert that string into JSON, we use JSON.parse() function, and this the main function that throws errors. Nearly all JSON.parse errors are a subset of SyntaxError error type. Debugging console throws around 32 different errors messages when you mess up your JSON data. And some of them are very tricky to debug; and yes I am talking about you unexpected non-whitespace character after JSON data.

Why the SyntaxError Horror?

SyntaxError is an inherited object of the main error object The main reason behind the error is usually a mistake in the JSON file syntax. You mess up and put a “ instead of a ‘ and you invite to face the SyntaxError JSON.parse: unexpected character.

Just like every programming language, a JSON file has a fixed syntax. Small syntax errors throws errors. For example, in the following code, i forgot to remove a trailing comma

JSON.parse(‘[a,b, c, d, e, f,]’);

Error1

Similarly, in this example I forgot to add }

JSON.parse(‘{«LambdaTest»: 1,’);

error2

How to Catch The Error Before Hand?

The problem with debugging JSON errors are that you get to know about one error at a time. Debuggers throw the first error they find and then stop. It would be upto you to regression find the bugs. Usually it’s not as difficult as it sounds.

The first step is to make sure that your JSON file is perfect from the get go. Here you can take help from JSON linting tools like cleverly named jsonlint.com

If you don’t have any control over the receiving JSON file, then the next step is to add catch exceptions around your JSON.parse.

function validatingJSON (json) {

  var checkedjson

  try {

    checkedjson = JSON.parse(json)

  } catch (e) {

  }

  return checkedjson }

Also, here are the main errors related to JSON.parse that I very painstakingly collected from a very single source [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/JSON_bad_parse] 😛

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

SyntaxError: JSON.parse: unterminated string literal

SyntaxError: JSON.parse: bad control character in string literal

SyntaxError: JSON.parse: bad character in string literal

SyntaxError: JSON.parse: bad Unicode escape

SyntaxError: JSON.parse: bad escape character

SyntaxError: JSON.parse: unterminated string

SyntaxError: JSON.parse: no number after minus sign

SyntaxError: JSON.parse: unexpected nondigit

SyntaxError: JSON.parse: missing digits after decimal point

SyntaxError: JSON.parse: unterminated fractional number

SyntaxError: JSON.parse: missing digits after exponent indicator

SyntaxError: JSON.parse: missing digits after exponent sign

SyntaxError: JSON.parse: exponent part is missing a number

SyntaxError: JSON.parse: unexpected end of data

SyntaxError: JSON.parse: unexpected keyword

SyntaxError: JSON.parse: unexpected character

SyntaxError: JSON.parse: end of data while reading object contents

SyntaxError: JSON.parse: expected property name or ‘}’

SyntaxError: JSON.parse: end of data when ‘,’ or ‘]’ was expected

SyntaxError: JSON.parse: expected ‘,’ or ‘]’ after array element

SyntaxError: JSON.parse: end of data when property name was expected

SyntaxError: JSON.parse: expected doublequoted property name

SyntaxError: JSON.parse: end of data after property name when ‘:’ was expected

SyntaxError: JSON.parse: expected ‘:’ after property name in object

SyntaxError: JSON.parse: end of data after property value in object

SyntaxError: JSON.parse: expected ‘,’ or ‘}’ after property value in object

SyntaxError: JSON.parse: expected ‘,’ or ‘}’ after propertyvalue pair in object literal

SyntaxError: JSON.parse: property names must be doublequoted strings

SyntaxError: JSON.parse: expected property name or ‘}’

SyntaxError: JSON.parse: unexpected character

SyntaxError: JSON.parse: unexpected nonwhitespace character after JSON data

If this doesn’t help, then you are in for one long debugging session. Leave a comment below with your issue. I can help.

Deeksha Agarwal

Deeksha Agarwal is in Product Growth at LambdaTest and is also a passionate tech blogger and product evangelist.

Author Profile
Author Profile
Author Profile

Author’s Profile

Deeksha Agarwal

Deeksha Agarwal is in Product Growth at LambdaTest and is also a passionate tech blogger and product evangelist.

Got Questions? Drop them on LambdaTest Community. Visit now

Test your websites, web-apps or mobile apps seamlessly with LambdaTest.

  • Selenium, Cypress, Playwright & Puppeteer Testing
  • Real Devices Cloud
  • Native App Testing
  • Appium Testing
  • Live Interactive Testing
  • Smart Visual UI Testing

Book a Demo

#LambdaTestYourBusiness- Holiday Season

The fact the character is a < make me think you have a PHP error, have you tried echoing all errors.

Since I don’t have your database, I’m going through your code trying to find errors, so far, I’ve updated your JS file

$("#register-form").submit(function (event) {

    var entrance = $(this).find('input[name="IsValid"]').val();
    var password = $(this).find('input[name="objPassword"]').val();
    var namesurname = $(this).find('input[name="objNameSurname"]').val();
    var email = $(this).find('input[name="objEmail"]').val();
    var gsm = $(this).find('input[name="objGsm"]').val();
    var adres = $(this).find('input[name="objAddress"]').val();
    var termsOk = $(this).find('input[name="objAcceptTerms"]').val();

    var formURL = $(this).attr("action");


    if (request) {
        request.abort(); // cancel if any process on pending
    }

    var postData = {
        "objAskGrant": entrance,
        "objPass": password,
        "objNameSurname": namesurname,
        "objEmail": email,
        "objGsm": parseInt(gsm),
        "objAdres": adres,
        "objTerms": termsOk
    };

    $.post(formURL,postData,function(data,status){
        console.log("Data: " + data + "nStatus: " + status);
    });

    event.preventDefault();
});

PHP Edit:

 if (isset($_POST)) {

    $fValid = clear($_POST['objAskGrant']);
    $fTerms = clear($_POST['objTerms']);

    if ($fValid) {
        $fPass = clear($_POST['objPass']);
        $fNameSurname = clear($_POST['objNameSurname']);
        $fMail = clear($_POST['objEmail']);
        $fGsm = clear(int($_POST['objGsm']));
        $fAddress = clear($_POST['objAdres']);
        $UserIpAddress = "hidden";
        $UserCityLocation = "hidden";
        $UserCountry = "hidden";

        $DateTime = new DateTime();
        $result = $date->format('d-m-Y-H:i:s');
        $krr = explode('-', $result);
        $resultDateTime = implode("", $krr);

        $data = array('error' => 'Yükleme Sırasında Hata Oluştu');

        $kayit = "INSERT INTO tbl_Records(UserNameSurname, UserMail, UserGsm, UserAddress, DateAdded, UserIp, UserCityLocation, UserCountry, IsChecked, GivenPasscode) VALUES ('$fNameSurname', '$fMail', '$fGsm', '$fAddress', '$resultDateTime', '$UserIpAddress', '$UserCityLocation', '$UserCountry', '$fTerms', '$fPass')";
        $retval = mysql_query( $kayit, $conn ); // Update with you connection details
            if ($retval) {
                $data = array('success' => 'Register Completed', 'postData' => $_POST);
            }

        } // valid ends
    }echo json_encode($data);

Join the DZone community and get the full member experience.

Join For Free

JSON, or JavaScript Object Notation, is a ubiquitous data format used by all sorts of mobile and web apps for asynchronous browser-server communication. JSON is an extremely popular data format, very easy to work with, compatible with every major programming language, and is supported by every major browser. However, just like any programming language, it throws a lot of errors when it decides that today is not going to be your day.

JSON.Parse Syntax Errors

In most web applications, nearly all data transferred from a web server is transmitted in a string format. To convert that string into JSON, we use the JSON.parse() function, and this is the main function that throws errors. Nearly all JSON.parse errors are a subset of the SyntaxError error type. The debugging console throws around 32 different error messages when you mess up your JSON data. And some of them are very tricky to debug; and yes I am talking about you unexpected non-whitespace character after JSON data.

Why the SyntaxError Horror?

SyntaxError is an inherited object of the main error object The main reason behind the error is usually a mistake in the JSON file syntax. You mess up and put a “ instead of a ‘ and you invite to face the SyntaxError JSON.parse: unexpected character.

Just like every programming language, a JSON file has a fixed syntax. Small syntax errors throws errors. For example, in the following code, I forgot to remove a trailing comma

JSON.parse('[a,b, c, d, e, f,]');

Error1

Similarly, in this example, I forgot to add the final } character.

JSON.parse('{"LambdaTest": 1,');

error2

How to Catch the Error Before Hand

The problem with debugging JSON errors is that you only get to know about one error at a time. Debuggers throw the first error they find and then stop. It would be up to you to regression find the bugs. Usually, it’s not as difficult as it sounds.

The first step is to make sure that your JSON file is perfect from the get go. Here you can take help from JSON linting tools like cleverly named jsonlint.com

If you don’t have any control over the receiving JSON file, then the next step is to add catch exceptions around your JSON.parse.

function validatingJSON (json) {

  var checkedjson

  try {
    checkedjson = JSON.parse(json)
  } catch (e) {

  }

  return checkedjson }

Also, here are the main errors related to JSON.parse that I very painstakingly collected from a very single source [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/JSON_bad_parse]:

SyntaxError: JSON.parse: unterminated string literal 
SyntaxError: JSON.parse: bad control character in string literal 
SyntaxError: JSON.parse: bad character in string literal 
SyntaxError: JSON.parse: bad Unicode escape 
SyntaxError: JSON.parse: bad escape character 
SyntaxError: JSON.parse: unterminated string 
SyntaxError: JSON.parse: no number after minus sign 
SyntaxError: JSON.parse: unexpected non-digit 
SyntaxError: JSON.parse: missing digits after decimal point 
SyntaxError: JSON.parse: unterminated fractional number 
SyntaxError: JSON.parse: missing digits after exponent indicator 
SyntaxError: JSON.parse: missing digits after exponent sign 
SyntaxError: JSON.parse: exponent part is missing a number 
SyntaxError: JSON.parse: unexpected end of data 
SyntaxError: JSON.parse: unexpected keyword 
SyntaxError: JSON.parse: unexpected character 
SyntaxError: JSON.parse: end of data while reading object contents 
SyntaxError: JSON.parse: expected property name or '}' 
SyntaxError: JSON.parse: end of data when ',' or ']' was expected 
SyntaxError: JSON.parse: expected ',' or ']' after array element 
SyntaxError: JSON.parse: end of data when property name was expected 
SyntaxError: JSON.parse: expected double-quoted property name 
SyntaxError: JSON.parse: end of data after property name when ':' was expected 
SyntaxError: JSON.parse: expected ':' after property name in object 
SyntaxError: JSON.parse: end of data after property value in object 
SyntaxError: JSON.parse: expected ',' or '}' after property value in object 
SyntaxError: JSON.parse: expected ',' or '}' after property-value pair in object literal 
SyntaxError: JSON.parse: property names must be double-quoted strings 
SyntaxError: JSON.parse: expected property name or '}' 
SyntaxError: JSON.parse: unexpected character 
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data

If this doesn’t help, then you are in for one long debugging session.

JSON

Improve Article

Save Article

  • Read
  • Discuss
  • Improve Article

    Save Article

    This JavaScript exception thrown by JSON.parse() occurs if string passed as a parameter to the method is invalid.

    Message:

    SyntaxError: JSON.parse: unterminated string literal
    SyntaxError: JSON.parse: bad control character in string literal
    SyntaxError: JSON.parse: bad character in string literal
    SyntaxError: JSON.parse: bad Unicode escape
    SyntaxError: JSON.parse: bad escape character
    SyntaxError: JSON.parse: unterminated string
    SyntaxError: JSON.parse: no number after minus sign
    SyntaxError: JSON.parse: unexpected non-digit
    SyntaxError: JSON.parse: missing digits after decimal point
    SyntaxError: JSON.parse: unterminated fractional number
    SyntaxError: JSON.parse: missing digits after exponent indicator
    SyntaxError: JSON.parse: missing digits after exponent sign
    SyntaxError: JSON.parse: exponent part is missing a number
    SyntaxError: JSON.parse: unexpected end of data
    SyntaxError: JSON.parse: unexpected keyword
    SyntaxError: JSON.parse: unexpected character
    SyntaxError: JSON.parse: end of data while reading object contents
    SyntaxError: JSON.parse: expected property name or '}'
    SyntaxError: JSON.parse: end of data when ',' or ']' was expected
    SyntaxError: JSON.parse: expected ',' or ']' after array element
    SyntaxError: JSON.parse: end of data when property name was expected
    SyntaxError: JSON.parse: expected double-quoted property name
    SyntaxError: JSON.parse: end of data after property name when ':' 
                 was expected
    SyntaxError: JSON.parse: expected ':' after property name in object
    SyntaxError: JSON.parse: end of data after property value in object
    SyntaxError: JSON.parse: expected ',' or '}' after property value in 
                 object
    SyntaxError: JSON.parse: expected ',' or '}' after property-value 
                 pair in object literal
    SyntaxError: JSON.parse: property names must be double-quoted strings
    SyntaxError: JSON.parse: expected property name or '}'
    SyntaxError: JSON.parse: unexpected character
    SyntaxError: JSON.parse: unexpected non-whitespace character after 
                 JSON data
    SyntaxError: JSON.parse Error: Invalid character at position {0} 
                 (Edge)
    

    Error Type:

    SyntaxError
    
    

    Cause of Error: This string passed to JSON.parse() method is invalid and will throw this error.

    Example 1:

    HTML

    <script>

        var str = '{"Prop_1" : "Val_1"}';

        JSON.parse(str);

        document.write(str);

    </script>

    Output:

    {"Prop_1" : "Val_1"}
    

    Example 2:

    HTML

    <script>

        var str = '{"Prop_1" : "Val_1"}}';

        JSON.parse(str);

        document.write(str);

    </script>

    Output(in console):

    Unexpected token } in JSON at position 2
    

    The JavaScript exceptions thrown by JSON.parse() occur when string failed to be parsed as JSON.

    Message

    SyntaxError: JSON.parse: unterminated string literal
    SyntaxError: JSON.parse: bad control character in string literal
    SyntaxError: JSON.parse: bad character in string literal
    SyntaxError: JSON.parse: bad Unicode escape
    SyntaxError: JSON.parse: bad escape character
    SyntaxError: JSON.parse: unterminated string
    SyntaxError: JSON.parse: no number after minus sign
    SyntaxError: JSON.parse: unexpected non-digit
    SyntaxError: JSON.parse: missing digits after decimal point
    SyntaxError: JSON.parse: unterminated fractional number
    SyntaxError: JSON.parse: missing digits after exponent indicator
    SyntaxError: JSON.parse: missing digits after exponent sign
    SyntaxError: JSON.parse: exponent part is missing a number
    SyntaxError: JSON.parse: unexpected end of data
    SyntaxError: JSON.parse: unexpected keyword
    SyntaxError: JSON.parse: unexpected character
    SyntaxError: JSON.parse: end of data while reading object contents
    SyntaxError: JSON.parse: expected property name or 
    SyntaxError: JSON.parse: end of data when 
    SyntaxError: JSON.parse: expected 
    SyntaxError: JSON.parse: end of data when property name was expected
    SyntaxError: JSON.parse: expected double-quoted property name
    SyntaxError: JSON.parse: end of data after property name when 
    SyntaxError: JSON.parse: expected 
    SyntaxError: JSON.parse: end of data after property value in object
    SyntaxError: JSON.parse: expected 
    SyntaxError: JSON.parse: expected 
    SyntaxError: JSON.parse: property names must be double-quoted strings
    SyntaxError: JSON.parse: expected property name or 
    SyntaxError: JSON.parse: unexpected character
    SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data
    

    Error type

    What went wrong?

    JSON.parse() parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered.

    Examples

    JSON.parse() does not allow trailing commas

    Both lines will throw a SyntaxError:

    JSON.parse('[1, 2, 3, 4,]');
    JSON.parse('{"foo": 1,}');
    
    
    

    Omit the trailing commas to parse the JSON correctly:

    JSON.parse('[1, 2, 3, 4]');
    JSON.parse('{"foo": 1}');
    

    Property names must be double-quoted strings

    You cannot use single-quotes around properties, like ‘foo’.

    JSON.parse("{'foo': 1}");
    
    
    

    Instead write «foo»:

    JSON.parse('{"foo": 1}');
    

    Leading zeros and decimal points

    You cannot use leading zeros, like 01, and decimal points must be followed by at least one digit.

    JSON.parse('{"foo": 01}');
    
    
    
    JSON.parse('{"foo": 1.}');
    
    
    

    Instead write just 1 without a zero and use at least one digit after a decimal point:

    JSON.parse('{"foo": 1}');
    JSON.parse('{"foo": 1.0}');
    

    See also

    • JSON
    • JSON.parse()
    • JSON.stringify()


    JavaScript

    • TypeError: invalid ‘instanceof’ operand ‘x’

      The JavaScript exception «invalid ‘instanceof’ operand» occurs when right-hand side operands operator isn’t used with constructor object, i.e.

    • TypeError: ‘x’ is not iterable

      The JavaScript exception «is not iterable» occurs when value which given right-hand side of for…of, argument function such Promise.all TypedArray.from,

    • SyntaxError: Malformed formal parameter

      The JavaScript exception «malformed formal parameter» occurs when argument list of Function() constructor call invalid somehow.

    • URIError: malformed URI sequence

      The JavaScript exception «malformed URI sequence» occurs when encoding decoding wasn’t successful.

    Понравилась статья? Поделить с друзьями:
  • Syntax error unexpected token joomla
  • Syntax error invalid syntax python ошибка
  • Syntax error invalid syntax python pip
  • Syntax error unexpected token in json at position 0 opencart
  • Syntax error invalid syntax perhaps you forgot a comma