Check if a String is Valid JSON String using JavaScript Last Updated : 28 Jun, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report To check if a string is a valid JSON string in JavaScript, one commonly used approach is with the JSON.parse() method, but we can also use alternative methods to ensure the string is valid. 1. Using JSON.parse() methodThe JSON.parse() method in JavaScript is used to parse a JSON string which is written in a JSON format and returns a JavaScript object. If the string is valid JSON, JSON.parse() will return a JavaScript object.SyntaxJSON.parse( text, function)text: The JSON string to be parsed.function: The reviver function that processes each key-value pair in the parsed object. JavaScript let s = '{ "name": "Sourav", "age": 23 }'; function isJSON(s) { try { JSON.parse(s); return true; } catch (e) { return false; } } console.log(isJSON(s)); Outputtrue In this exampleThe isJSON function tries to parse the string. If it succeeds, it returns true; otherwise, it catches the error and returns false.2. Using JSON.stringify() methodThe JSON.stringify() method in JavaScript is used to convert a JavaScript object or value into a JSON string. This is useful when you need to send data over a network (like to a server) or store it in a file, as JSON strings are a widely-used data format.SyntaxJSON.stringify(value, replacer, space);value: The data (object, array, etc.) to be converted.replacer: (Optional) Function or array to filter or modify the values.space: (Optional) Number or string to add indentation for readability. JavaScript let s = '{ "name": "Sourav", "age": 23 }'; function isJSON(s) { try { JSON.stringify(JSON.parse(s)); return true; } catch (e) { return false; } } console.log(isJSON(s)); Outputtrue In this example The isJSON(s) function attempts to parse the string s with JSON.parse() and then convert it back to a JSON string with JSON.stringify().If both operations succeed without errors, it returns true, indicating the string is valid JSON.If an error occurs during parsing or stringifying, it returns false, indicating the string is invalid JSON.3. Using Lodash _.isJSON() MethodIn Lodash _.isJSON() Method approach, we are using Lodash _.isJSON() method that returns the boolean value that is true if the passes value is a JSON string else it returns false. JavaScript let _ = require('lodash-contrib'); console.log("The Value is JSON : " +_.isJSON( '{"GeeksforGeeks" : "A Computer Science portal for Geeks"}')); Output:The Value is JSON : trueIn this exampleLodash-Contrib is a library that extends Lodash with additional utility functions, including _.isJSON().The code requires Lodash-Contrib using let _ = require('lodash-contrib');._.isJSON() is used to check if the input string is valid JSON.The string {"GeeksforGeeks" : "A Computer Science portal for Geeks"} is passed to _.isJSON().If the string is a valid JSON, it will return true; otherwise, false.4. Using Regular ExpressionIn JavaScript, you can use Regular Expressions (RegExp) to check if a string is in valid JSON format. This method works by matching the general structure of a JSON string rather than parsing it. JavaScript let s = '{ "name": "Sourav", "age": 23 }'; const jsonRegex = /^[\],:{}\s]*$/.test(s.replace(/\\["\\\/bfnrtu]/g, '@') .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')); console.log(jsonRegex); Outputtrue In this exampleThe code uses a regular expression to check if the string matches the structure of valid JSON.It replaces escape characters, strings, booleans, numbers, and other components of JSON with placeholders for simplified matching.The .test() method checks if the processed string follows the JSON format, returning true if valid, otherwise false. Comment More infoAdvertise with us Next Article Check if a String is Valid JSON String using JavaScript P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Questions +1 More Similar Reads JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version 7 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Introduction to Tree Data Structure Tree data structure is a hierarchical structure that is used to represent and organize data in the form of parent child relationship. The following are some real world situations which are naturally a tree.Folder structure in an operating system.Tag structure in an HTML (root tag the as html tag) or 15+ min read Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w 8 min read HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML 14 min read NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net 15+ min read Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel 7 min read Like