modification in js code

Dear all,
The code in http://jsfiddle.net/VM3jc/5/ 
assume following json string
[
	{
		"TableName": "TableName1",
		"languages": [
			{
				"name": "ID",
				"column_type": type2
			},
			{
				"name": "NameAR",
				"column_type": "type1"
			}
		]
	},
	{
		"TableName": "Tabs",
		"languages": [
			{
				"name": "FieldNameID",
				"column_type": type2
			},
			{
				"name": "FieldName02",
				"column_type": "type1"
			},
			{
				"name": "FieldName03",
				"column_type": "type1"
			}
		]
	},
	{
		"TableName": "TablName2",
		"languages": [
			{
				"name": "CustomerID",
				"column_type": type2
			},
			{
				"name": "FullName",
				"column_type": "type1"
			},
			{
				"name": "Address",
				"column_type": "type1"
			}
		]
	}
]

Open in new window


I need to modify the code to accept same json string but without "TableName" element
and add default on "TableName": "TablNameDefault"
Thanks.
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... Again :)

I didn't understand what you need here and how this relates to the sample.
Before you had a string and wanted to convert it to json.
Now you have json and want to do what?

Please give me an exact sample of the input and the expected output.
0
ethar1Author Commented:
still,  json parser assume the string have TableName:

I need to deal with a string contains no "TableName:"
and add it ( programmatically ) to the json
0
ethar1Author Commented:
can jsonfied add "TableName": "TablNameDefault" if its not shown in the string?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Alexandre SimõesManager / Technology SpecialistCommented:
How do you know when a new table begins?
Now your input string is something like:
Tablename1:
FieldNameID,typ1
FieldName02,typ2
FieldName03,typ3
Tablename:
FieldNameID,typ4
FieldName02,typ1
FieldName03,typ2

Open in new window

Like this I know that each time I find something that ends with : it's a table name. If I don't have this line how do I know which fields belong to what?
0
ethar1Author Commented:
if no Tablename1:
consider all as one table

if

FieldNameID,typ1
FieldName02,typ2
FieldName03,typ3
FieldNameID,typ4
FieldName02,typ1
FieldName03,typ2 

Open in new window

just treated it as :
TablNameDefault:
FieldNameID,typ1
FieldName02,typ2
FieldName03,typ3
FieldNameID,typ4
FieldName02,typ1
FieldName03,typ2 

Open in new window

0
Alexandre SimõesManager / Technology SpecialistCommented:
Ok mate, here you go: http://jsfiddle.net/VM3jc/6/
var jsonfied = function() {
    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 {
            if(!item) 
                item = { tableName: 'TablNameDefault', languages: [] };
            
            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

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
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.