Handling backslash when parsing json


Here is a portion of a larger JSON string that I attempting to call JSON.parse on. I am getting the infamous 'invalid character' error because (I believe) of the backslash parentheses <em>(ie. "path:\"<a href="https://mysite.sharepoint.com/sites/Test" rel="nofollow">https://mysite.sharepoint.com/sites/Test</a>\")</em>.

All online parsers I have tried it on works fine but in my javascript code the JSON.parse method fails.

I have attempted to clean the string like this and other ways but I have been unable to get it to parse.

var cleanData = data.replace(/\\"/, /\\\\/);

below is the partial JSON file. When I remove it from the JSON string the JSON.parse works so I think I have this isolated to just this. What type of general purpose clean method would work to get this thing to parse? Thansk

'{"Properties" : { "GenerationId" : 9223372036854776000, "indexSystem" : "", "ExecutionTimeMs" : 109, "QueryModification" : "path:\"https://mysite.sharepoint.com/sites/Test\" (IsDocument:\"True\" OR contentclass:\"STS_ListItem\") ContentTypeId:0x0120D5200098CBB075E51C8C4398ECCB4B4928912D*", "RenderTemplateId" : "~sitecollection/_catalogs/masterpage/Display Templates/Search/Group_Default.js", "StartRecord" : 0, "piPageImpressionBlockType" : 2 }}



The problem is that your backslash is getting swallowed as an escape character in the string:

'\"' === '"' // true

You actually need to escape the backslashes, so that the JSON parser sees them. Here's another example:

var unencoded = 'string with "quotes"'; '"string with \"quotes\""' === JSON.stringify(unencoded); // false '"string with \\"quotes\\""' === JSON.stringify(unencoded); // true

However, where the escaping should be done depends on how the JSON is being made available to the JavaScript. If the JSON is embedded in the page by a server-side script, then there's no need to use JSON.parse, as valid JSON is valid JavaScript:

// if JsonData is valid JSON, it's also a valid JavaScript object var data = <%= JsonData %>;


  • How to include a remote JavaScript file in a shiny dashboard app?
  • R Tree With n Branches
  • JPA @JoinTable - Three ID Columns
  • Are there any fluent WPF projects? [closed]
  • ADO.NET provider with invariant name 'System.Data.SqlClient;' cannot be found (Entity Fram
  • Fast crosstabs and stats on all pairs of variables
  • Facebook Logout Confusion
  • Playing a monetized YouTube song inside of a Google Chrome Extension. Do I have any options?
  • How to Configure Log4Net Custom Object Renderer for Generic Objects?
  • Unable to gem install nokogiri
  • Is there a equivalent to JSON.Net in Java? [duplicate]
  • end daemon processes with multiprocessing module
  • Creating a Multidimensional, Associative Array in VBScript
  • Tamper-proof configuration files in .NET?
  • Unable to install Git-core+svn by MacPorts
  • Unable to decode certificate at client new X509Certificate2()
  • print() is showing quotation marks in results
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • Launch Runnable Jar from Web Start
  • Bug in WPF DataGrid
  • Updating server-side rendering client-side
  • Javascript + PHP Encryption with pidCrypt
  • WOWZA + RTMP + HTML5 Playback?
  • How to delete a row from a dynamic generate table using jquery?
  • Free memory of cv::Mat loaded using FileStorage API
  • using HTMLImports.whenReady not working in chrome
  • How to set the response of a form post action to a iframe source?
  • Angular 2 constructor injection vs direct access
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • embed rChart in Markdown
  • How does Linux kernel interrupt the application?
  • Authorize attributes not working in MVC 4
  • How can I remove ASP.NET Designer.cs files?
  • EntityFramework adding new object to nested object collection
  • Programmatically clearing map cache
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal