What you're describing isn't something that can just be converted to JSON. What you're describing is the hierarchy to the object, sure, but with those braces in there, you're obviously looking at but one piece of a much larger object. However, for a parser to turn that string into a javascript object, we can do this:
function splitStringToObject(string){
var sourceArray = string.split('.');
var top = {};
var point = top;
while (sourceArray.length){
var work = sourceArray.shift();
if ( /([a-zA-Z_][a-zA-Z0-9_]*)\[([a-zA-Z0-9_]+)\]/.test(work) ){
console.log('found match alpha index')
//found an array identifier with a variable name inside ('bbb[aaa]')
var matches = /([a-zA-Z_][a-zA-Z0-9_]*)\[([a-zA-Z0-9_]+)\]/.exec(work);
var matchName = matches[1];
var matchIndex = matches[2];
point[matchName] = [];
point[matchName][matchIndex] = {};
point = point[matchName][matchIndex];
} else if ( /([a-zA-Z_][a-zA-Z0-9_]*)\[([0-9]+)\]/.test(work) ) {
console.log('found match numeric index')
//found an array identifier with a numeric index inside ('bbb[0]')
var matches = /([a-zA-Z_][a-zA-Z0-9_]*)\[([a-zA-Z0-9_]+)\]/.exec(work);
var matchName = matches[1];
var matchIndex = matches[2];
point[matchName] = [];
point[matchName][matchIndex] = {};
point = point[matchName][matchIndex];
} else if ( work.indexOf('[') > -1 || work.indexOf(']') > -1 ) {
console.log('found bad egg with ' + work)
} else if ( work.indexOf('=') > 0 ) {
console.log('found = inside')
//test for equals sign in the string
var morework = work.split('=');
work = morework[0].trim();
sourceArray.push('='+morework[1])
point[work] = morework[1].trim();
point = point[work];
} else {
console.log('found plain word')
//assume work is aok to use as another object here.
work = work.trim();
point[work] = {};
point = point[work];
}
}
//you may not want this next part
var ret;
//let's pull our value off the top, altho if we do this, I don't know
//how to retain the name. I prefer to return it under top still
for (var k in top){
ret = top[k];
}
console.log(ret);
return ret;
// alternately call
return top;
}
However, this is just a parser, how do we use that? Well, we need to feed it our strings. I'm going to assume you can neatly get all your strings into an array, like this:
var strings = [
"AAA.BBB[0].CCC.DDD[1].EEE = 123",
"AAA.BBB[0].CCC.DDD[2].EEE = 123",
"AAA.BBB[0].CCC.DDD[4].EEE = 123",
"AAA.BBB[0].CCC.DDD[5].EEE = 123",
];
and then the next part becomes really easy, as we see here:
var objectsConverted = [];
for(var k in strings){
objectsConverted[k] = splitStringToObject(strings[k]);
}
var result = {};
for(var k in objectsConverted){
jQuery.extend(true,result,objectsConverted[k]);
}
console.log(result);
and at the end of the day if I JSON.stringify(result) I get:
"{"BBB":[{"CCC":{"DDD":[null,{"EEE":"123"},{"EEE":"123"},null,{"EEE":"123"},{"EEE":"123"}]}}]}"
PLEASE read the caveat inside the end of the function, the namespace is lost because of the choice of return method (I'm returning from the top instead of nested once down)
AAA.BBB[0].CCC.DDD[5].EEE
mean? – Rocket Hazmat