JSON.parse解析失败但$.parseJSON可正常解析、后续JSON.stringify报错的异常问题排查
Let's break down what's happening here—this error almost never means JSON.parse actually isn't a function. It's a clear sign something is messing with the global JSON object in your Chrome environment.
First, Rule Out JSON Syntax Issues
Your JSON string (after replacing " with regular quotes) is completely valid:
[{ "id" : "3", "parent" : "2", "text" : "Lack of Survey Information" },{ "id" : "4", "parent" : "3", "text" : "Lack of Surveyor Resource" },{ "id" : "5", "parent" : "3", "text" : "Land access unavailable" },{ "id" : "2", "parent" : "1", "text" : "Design Incorrect" },{ "id" : "1", "parent" : "16", "text" : "Abortive Work" },{ "id" : "6", "parent" : "1", "text" : "Wrong design info used in construction" },{ "id" : "16", "parent" : "#", "text" : "a" }]
The # character in the parent field is totally allowed in JSON strings, which is why your JSFiddle test worked fine. Syntax isn't the problem here.
Why $.parseJSON Works But JSON.parse Doesn't
jQuery's $.parseJSON uses its own internal parsing logic (it doesn't rely directly on the global JSON object, especially in older jQuery versions). That's why it can bypass whatever's broken with your global JSON setup.
The real issue is that somewhere in your code or a third-party script, the global JSON object is being overwritten or modified. Common scenarios include:
- You accidentally declared a variable named
JSON(e.g.,var JSON = "some string";orconst JSON = {};), which replaces the native globalJSONobject. - A third-party script is modifying the
JSONobject in a way that breaks itsparseorstringifymethods.
When this happens, JSON is no longer the native object with standard methods—so calling JSON.parse() throws that confusing "not a function" error, and JSON.stringify() crashes for the same reason.
How to Verify and Fix This
Check what
JSONactually is in your environment:
Open Chrome's DevTools console right before the line that callsJSON.parse, and run:console.log(JSON); console.log(typeof JSON);If you see a string, custom object, or anything other than the native
JSONobject (which should show up asJSON { parse: ƒ, stringify: ƒ, ... }), you've found the culprit.Hunt down the code overriding JSON:
- Search your codebase for any lines declaring
var JSON,let JSON, orconst JSON. Rename those variables immediately—never useJSONas a variable name. - Check third-party scripts you're loading (like old plugins or analytics tools). If one is modifying the global
JSONobject, consider updating it to a newer version, replacing it with an alternative, or wrapping your code in an IIFE (Immediately Invoked Function Expression) to isolate your scope from global pollution.
- Search your codebase for any lines declaring
Test in a clean environment:
Run just the JSON parsing code in a fresh Chrome tab (without any of your project's scripts loaded). It should work fine, confirming the problem is specific to your project's code/script setup.
内容的提问来源于stack exchange,提问作者Andrew Watts

