How Does the JSON.parse() Method Works in JavaScript? Last Updated : 21 Dec, 2024 Comments Improve Suggest changes Like Article Like Report The JSON.parse() method in JavaScript is used to convert a JSON string into a JavaScript object. This method is essential for working with JSON data, especially when fetching or processing data from APIs or external sources.Converts a JSON-formatted string into a JavaScript object.Maintains the structure of arrays, nested objects, and data types like numbers and strings.Supports a reviver function to transform values during parsing.It throws an error if the input JSON string is invalid. JavaScript const jsonS = '{"name": "Aarav", "age": 22, "city": "Delhi"}'; const obj = JSON.parse(jsonS); console.log(obj.name); OutputAarav jsonString is a valid JSON string.JSON.parse() converts the string into a JavaScript object.Object properties are accessed using dot notation.How the JSON.parse() Method Works1. Parsing Simple JSON StringsThe method processes JSON strings and maps key-value pairs to a JavaScript object. JavaScript const jsonS = '{"product": "Mobile", "price": 12000}'; const obj = JSON.parse(jsonS); console.log(obj.product); OutputMobile 2. Handling JSON ArraysIt seamlessly parses JSON strings representing arrays into JavaScript arrays. JavaScript const jsonA = '[{"name": "Riya"}, {"name": "Karan"}]'; const a = JSON.parse(jsonA); console.log(a[0].name); OutputRiya 3. Parsing Nested JSONThe method processes deeply nested JSON objects. JavaScript const nJson = '{"user": {"name": "Simran", "address": {"city": "Pune", "pin": 411001}}}'; const obj = JSON.parse(nJson); console.log(obj.user.address.city); OutputPune 4. Validating JSON During ParsingInvalid JSON strings throw a SyntaxError. You can use a try...catch block to handle these cases. JavaScript const invalidJson = '{"name": "Ajay", "age": 30'; try { JSON.parse(invalidJson); } catch (e) { console.error("Invalid JSON:", e.message); } 5. Using a Reviver FunctionThe reviver parameter modifies or filters values during the parsing process. JavaScript const jsonS = '{"price": "1500", "discount": "5"}'; const obj = JSON.parse(jsonS, (key, value) => { if (key === "price" || key === "discount") return parseFloat(value); return value; }); console.log(obj.price - obj.discount); Output1495 Comment More infoAdvertise with us Next Article Company Preparation A amanv09 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-QnA JavaScript-JSON Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like