Shubham vyas Shubham vyas - 9 months ago 63
AngularJS Question

angularjs - how to convert large string object into chunk of key/value pair

I took data from a text file i.e sample_resume.txt

Name: John Doe
Phone: (555) 555-5555

Excel in a web developer career.

Development: HTML5, JavaScript, Bootstrap, AngularJS, ReactJS, CSS3, Media Queries, Development
Project Management: JIRA, Bitbucket, Confluence, Git, GitHub

Title: Junior Web Developer
Company: Apple Inc.
Dates: June 2015 to September 2016
* Developed responsive corporate websites
* Did some cool stuff
* Led team in closing out JIRA bugs

and imported it into my angular project using a controller
app.controller('MainController', function ($scope, $http) {

.then(function (data) {
$ = data;

}, function (error) {



I expected the output in Json format to be divided in to key value pairs of the data but the output is

{"data":"Name: John Doe \r\nPhone: (555) 555-5555 \r\nEmail:\r\n\r\nOBJECTIVE \r\nExcel in a web developer career.\r\n\r\nKEY SKILLS\r\nDevelopment: HTML5, JavaScript, Bootstrap, AngularJS, ReactJS, CSS3, Media Queries, Development\r\nProject Management: JIRA, Bitbucket, Confluence, Git, GitHub\r\n\r\nEMPLOYMENT HISTORY\r\nTitle: Junior Web Developer\r\nCompany: Apple Inc. \r\nDates: June 2015 to September 2016\r\n* Developed responsive corporate websites\r\n* Did some cool stuff\r\n* Led team in closing out JIRA bugs\r\n\r\nTitle: Web Development Intern \r\nCompany: Google Inc.\r\nDates: January 2015 to May 2015\r\n* Went on coffee runs for the team\r\n* Team record for longest keg stand\r\n* Once ate 82 cupcakes during a team building event\r\n\r\nEDUCATION\r\nDegree: BBA \r\nSchool: Michigan State University\r\nGPA: 2.2\r\nMajor: Computer Science\r\nMinor: Drinking","status":200,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"url":"../../documents/sample_resume.txt","headers":{"Accept":"application/json, text/plain, /"}},"statusText":"OK"}

i need the data, split into key value pairs like:
{"Name": "John Doe",
Phone: (555) 555-5555}

Answer Source

Ok, if parsing the text into an array of key value pairs, splitted by the ":" in the text, this code will do what you want. However, if you actually control this text file output, I would consider making it a json file with exact structure you need.

I am asuming AngularJS here, hence putting the "kvp" array on the scope.

var lines ='\r\n'); //split by the newlines
$scope.kvp = [];
lines.forEach(function(line, index) {
  var pair = line.split(':'); //split by colon
  if (pair.length === 2) { //do we have a pair?
      key: pair[0],
      value: pair[1]
  } else {
    if (index > 0) {
      if ($scope.kvp[$scope.kvp.length - 1]) {
        $scope.kvp[$scope.kvp.length - 1].value += pair[0]; //append last known pair

Here is a working JSFiddle: