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

98
Views
How to parse an array inside a string into a javascript arrray

I have a string received from a database call as follows:

"[{'url':'https://www.example.com/','category':'popular'},{'url':'https://example2.com/','category':'new'}]"

I would like to parse the string into a typescript/JS array so that it is as follows:

[
   {'url':'https://www.example.com/','category':'popular'}, 
   {'url':'https://example2.com/','category':'new'}
]

How can I go about doing this? JSON.parse won't work as the string does not resemble a stringified JSON. Thanks!

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

0

Assuming there are no strings in it containing ', you can replace all occurrences of the single quote with double quotes, then you can parse it:

const str = "[{'url':'https://www.example.com/','category':'popular'},{'url':'https://example2.com/','category':'new'}]";

const arr = JSON.parse(str.replace(/'/g,'"'));

console.log(arr);

about 4 years ago · Juan Pablo Isaza Report

0

Replace single quotes with double quotes then JSON.parse

JSON.parse(
"[{'url':'https://www.example.com/','category':'popular'},{'url':'https://example2.com/','category':'new'}]"
.replace(/\'/g, '"')
)
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!