Jenz
Jenz

Reputation: 8369

Read a text file and convert to JSON in a particular format

I am reading employee details from a text file like shown below.

$( document ).ready(function() {
    $.ajax({
        url: "employees.txt",
        success:function(response) {
            console.log(response);
        }
    });
});

It gives me response like :

    Mark
    Mark have 10 years of experience working with youth agencies. Mark have a bachelor’s degree in outdoor education. He raise money, train leaders, and organize units. He have raised over $100,000 each of the last six years. He consider himself a good public speaker, and have a good sense of humor.

    Jennifer
    Jennifer enjoy meeting new people and finding ways to help them have an uplifting experience. She have had a variety of customer service opportunities, through which she was able to have fewer returned products and increased repeat customers, when compared with co-workers. She is dedicated, outgoing, and a team player.

Now from this response I need a resultant structure like :

 var employees = [
   ["mark", {
      "name": "Mark",
      "description": "Mark have 10 years of experience working with youth agencies. Mark have a bachelor’s degree in outdoor education. He raise money, train leaders, and organize units. He have raised over $100,000 each of the last six years. He consider himself a good public speaker, and have a good sense of humor."
   }],
   ["jennifer", {
      "name": "Jennifer",
      "description": "Jennifer enjoy meeting new people and finding ways to help them have an uplifting experience. She have had a variety of customer service opportunities, through which she was able to have fewer returned products and increased repeat customers, when compared with co-workers. She is dedicated, outgoing, and a team player."
   }]
];

How can I do this? Can anyone help me to do this? Thanks in advance.

Upvotes: 1

Views: 3199

Answers (1)

Mamdouh Saeed
Mamdouh Saeed

Reputation: 2324

This example tested with div has your text in HTML and when we get innerText this returns blocks that we can split it by \n\n

//split using \n\n

function toJson(str) {
  var tt = [];
  var rw = str.split("\n\n");
  for (var i = 0; i < rw.length; i++) {
    var name = rw[i].split("\n")[0].trim();
    var description = rw[i].split("\n")[1].trim();
    var jsn = [
      name, {
        "name": name,
        "description": description
      }
    ]
    tt.push(jsn);
  }
  return tt;
}

var employees = toJson(document.getElementById("txt").innerText);

console.log(employees);
<div id='txt'>
  Mark
  <br/>Mark have 10 years of experience working with youth agencies. Mark have a bachelor’s degree in outdoor education. He raise money, train leaders, and organize units. He have raised over $100,000 each of the last six years. He consider himself a good
  public speaker, and have a good sense of humor.
  <br/>
  <br/>Jennifer
  <br/>Jennifer enjoy meeting new people and finding ways to help them have an uplifting experience. She have had a variety of customer service opportunities, through which she was able to have fewer returned products and increased repeat customers, when compared
  with co-workers. She is dedicated, outgoing, and a team player.
</div>

Upvotes: 1

Related Questions