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

201
Views
How to create multiple objects in JSON from input in textarea?

I have a problem. My users can write things down in the textarea and I would like to make sure that the first line in the WYISWYG editor is an object in JSON, the second line is an object in the WYISYG and so on.

What is the actual problem?

I need to separately style the three lines, so I need to get the elements one by one as an output. The problem is that now I have one object with a long string that I have to divide in three lines, which I can't do.

To illustrate what I mean:

Textarea --> someone writes:

  1. Hello
  2. I am an example
  3. Hello 2

To get:

{"line 1":"<p>Hello<\/p>\"}
{"line 2":"<p>I am an example<\/p>\"}
{"line 3":"<p>Hello 2<\/p>\"}

Instead I get:

{"line 1":"<p>Hello<\/p>\n<p>I am an example<\/p>\n<p>Hello 2<\/p>\n"}

Could anyone help here?

Edit: I am parsing it like this:

openingtimes = parse(json["line1"] ?? "").documentElement!.text;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

After getting the contents of the textarea, you can use .split("\n") to get the array of separate lines. (Note the use of " instead of '.)

If you then need to convert that array into an object, just create an empty object, loop through the lines array and add them to it.

Have a look at the different console.log()s output at every stage:

function getTextareaContentsAsArray() {
    // Get the element
    let textarea = document.getElementById('the_editor');
    
    // Get the contents
    let contents = textarea.value;
    
    // Split by line ending
    let lines_array = contents.split("\n");
    console.log(lines_array);

    // Convert array to array of JSON
    let lines_object = {};
    lines_array
        // Remove empty lines
        .filter((v) => v.length)
        // Loop through all lines and add them to the_json object
        .map((line, index) => {
            lines_object[`line${index+1}`] = line;
        });
    console.log(lines_object);
    
    let json_as_string = JSON.stringify(lines_object);
    console.log(json_as_string);
}
<textarea id="the_editor" cols="50" rows="5"><p>Hello</p>
<p>I am an example</p>
<p>Hello 2</p>
</textarea>

<button id="the_button" onclick="getTextareaContentsAsArray()">Get contents as array of lines</button>

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!