DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Streamlining Event Data in Event-Driven Ansible
  • Dynamic Web Forms In React For Enterprise Platforms
  • Unlocking Oracle 23 AI's JSON Relational Duality
  • Loading XML into MongoDB

Trending

  • How to Format Articles for DZone
  • Testing SingleStore's MCP Server
  • AI's Dilemma: When to Retrain and When to Unlearn?
  • How To Replicate Oracle Data to BigQuery With Google Cloud Datastream
  1. DZone
  2. Coding
  3. Languages
  4. SyntaxError: JSON.parse: bad parsing

SyntaxError: JSON.parse: bad parsing

Tired of getting this annoying error when your parsing your JSON? Read on to learn how!

By 
Deeksha Agarwal user avatar
Deeksha Agarwal
·
Feb. 25, 19 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
62.2K Views

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

Published at DZone with permission of Deeksha Agarwal. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Streamlining Event Data in Event-Driven Ansible
  • Dynamic Web Forms In React For Enterprise Platforms
  • Unlocking Oracle 23 AI's JSON Relational Duality
  • Loading XML into MongoDB

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!