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

165
Views
Escape json using \" in javascript

I have this input {"a":["b","c","d"]}

For whatever reason, I need output like this "{\"a\":[\"b\",\"c\",\"d\"]}"

Instead of this (using JSON.stringify()) '{"a":["b","c","d"]}'

I know I can write some replacements or something. Is there any native Javascript method to do this?

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

0

The problem with using a custom replace is that you risk making the string invalid JSON. Instead you can simply stringify it twice, which will properly escape all special characters such that it can be reliably parsed back to a valid javascript object.

const input = { 'a': ['A string with "quotes"', "c", "d"] };

const string = JSON.stringify(JSON.stringify(input));
console.log(string);

const parsed = JSON.parse(JSON.parse(string));
console.log(parsed);

about 4 years ago · Juan Pablo Isaza Report

0

Try this one

const a = {"a":["b","c","d"]}
const output = JSON.stringify(a).replaceAll('"', '\\"')
console.log(output)

Hope this helps

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!