Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

83
Views
JSON parse double quotes

I encountered a very strange error. If I run JSON.Parse('[""d""]') then I have error: "Unexpected token d in JSON at position 3"

JSON.parse('["\"d\""]')

If I run console.log(JSON.stringify(['"d"'])) then I get '[""d""]'

console.log(JSON.stringify(['"d"']))

If I run console.log(JSON.parse(JSON.stringify(['"d"']))) then I get good result [""d""]

console.log(JSON.parse(JSON.stringify(['"d"'])))

If I use eval function then I have error: "Uncaught SyntaxError: Unexpected token d in JSON at position 3"

var someJson=JSON.stringify(['"d"']);
window.eval(`JSON.parse('${someJson}')`)

How fix it?

I use some external library and there is window.eval(JSON.parse('${JSON.stringify(t)}'))). Where t - json array with strings. String can contains double quoutes. It throws exception.

I found js changed my string value when I set it.

var someJson='["\"d\""]';
console.log(someJson);

It returns '[""d""]' and when I run JSON.Parse it is incorrect JSON.

How fix it?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

JSON.parse('["\"d\""]')

\ is a special character in JS strings, so to include it in your JSON you need to escape it.

const yourString = '["\"d\""]';
const validJSON = '["\\"d\\""]'
const parsedData = JSON.parse(validJSON);

document.querySelector('#yourString').value = yourString;
document.querySelector('#validJSON').value = validJSON;
<p>Your string: <output id="yourString"></output>
<p>Valid JSON: <output id="validJSON"></output>

As a rule of thumb, embedding JSON in string literals is a waste of time and effort. You can just write the JS data structure you get from parsing the JSON.

about 4 years ago · Juan Pablo Isaza Report

0

@Quentin many thanks for idea! Result answer for me:

var someJson=JSON.stringify(['"d"']);
console.log(window.eval(`JSON.parse('${someJson.replaceAll('\\','\\\\')}')`))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!