Generate Json using JS

Deal all,
The following code  generate json
var jsonfied = [{
		items : items.replace(/,$/, "").split("\n").map(function(item) {
                        var values = item.split(',');
			return {
				name : values[0],
                                column_type: values[1]
			};
		})
	}];

Open in new window

to parse this data :
FieldName01,typ1
FieldName02,typ2
FieldName03,typ3

Open in new window


I just need to change the js code to pares this data:
Tablename1:
FieldNameID,typ1
FieldName02,typ2
FieldName03,typ3
Tablename2:
FieldNameID,typ4
FieldName02,typ1
FieldName03,typ2

Open in new window

and generate the following json:
[{
      TableName: "TableName1" ,
      languages: [
      { name: "FieldNameID" , column_type:"typ1"  },
      { name: "FieldName02" , column_type:"typ2" },
      { name: "FieldName03" , column_type:"typ3"  },

      ],
},
{
      TableName: "TableName2" ,
      languages: [
      { name: "FieldNameID" , column_type:"typ4" },
      { name: "FieldName02" , column_type:"typ1" },
      { name: "FieldName03" , column_type:"typ2" },

      ]
}]

Open in new window

ethar1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Technology SpecialistCommented:
Hi mate, here it is: http://jsfiddle.net/VM3jc/3/
This time I didn't do it with the map because there's no way to properly split by the table name row.
So what I do is split by line and create a new item each time I find a line that ends with :


var items = 'Tablename1:\nFieldNameID,typ1\nFieldName02,typ2\nFieldName03,typ3\nTablename:\nFieldNameID,typ4\nFieldName02,typ1\nFieldName03,typ2';

var jsonfied = function(input) {
    var result = [];
    var lines = items.replace(/,$/, "").split("\n");
 
    var item = null;
    for (var i=0;i<lines.length;i++){
        if(lines[i].substr(-1) === ':'){
            if(item) result.push(item);
            item = { tableName: lines[i].split(':')[0], languages: [] };
        } else {
            var values = lines[i].split(',');
            item.languages.push({ name: values[0], column_type: values[1] });
        }
    }
    
    if(item) result.push(item);
    return result;
}

Open in new window

0
ethar1Author Commented:
what is the useful of input here?
0
Alexandre SimõesManager / Technology SpecialistCommented:
I added it just to make the function independent of an external variable but then I ended up not using it, sorry.
Here's the sample without it: http://jsfiddle.net/VM3jc/5/
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ethar1Author Commented:
ok, I just need to add [ and ] for whole result string ?
0
ethar1Author Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.