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

322
Views
convert string to object from html input element

I have a textarea element which takes object types as an input like

{
  name: "root",
  backlog: [
    {name: "log#1"},
  ]
}

Accessing the data returns it as a string

Is there a simple way to convert the String to that specific javascript object without using regex filters? Just removing the outer quotation marks?

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

0

use json5 or Relaxed JSON library.

here is example using json5 library

let string = `  {
    name: "root",
    backlog: [
      {name: "log#1"},
      ]
    }`;
    
 let object = JSON5.parse(string);
 console.log(object)
<script src="https://unpkg.com/json5@^2.0.0/dist/index.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report

0

To convert a string to a JSON, use: JSON.parse([the input]).

And, to convert it back to a string: JSON.stringify([the input]).

about 4 years ago · Juan Pablo Isaza Report

0

If i understand you correctly :

const string = '{ name: "root", backlog: [{name: "log#1"}]}'
const jsonStr = string.replace(/(\w+:)|(\w+ :)/g, function(matchedStr) {
  return '"' + matchedStr.substring(0, matchedStr.length - 1) + '":';
});

const result = JSON.parse(jsonStr)
console.log(result)

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!