I will start by saying I do know how to use the javascript JSON library, my question is not as simple as that, so please do read the detail not just the title, as I couldn't express it well in the title...
What I am trying to achieve is converting a csv to a JSON document but using a dynamic schema to map the varying schemas of the csv's to a defined document schema
I have a JSON document that has string keys i,e "Field1", "Field2", etc.... the corresponding value is a pipe delimited string that contains the type of document before the first delimiter and then each string within the delimiters builds up a "Path" of the dynamic json document I want to create...
So the csv comes in and is converted to json....
"Field1" in the uploaded CSV contains the value "Potatoes"
We retrieve the pipe delimited string for "Field1" which has the value "1|Vegetables|RootVeg"
Therefore dynamically I need to build the json document up so that for this field the corresponding document section would look like
{ "Vegetables" : { "RootVeg" : "Potatoes"} }
Hopefully that makes sense.... Below is the various foreach loops I am trying to build this up with....
var vesselprimary = [];
var vesselsecondary = [];
angular.forEach(value, function (value2, key2) {
var pipedValue = data[0][key2];
console.log("pipedvalue", pipedValue);
console.log("value2", value2);
var arrayValues = pipedValue.split("|");
if (arrayValues[0] === 1) {
//Create the VesselPrimary JSON object to put into the body of the post
if (arrayValues.count > 2) {
vesselprimary[arrayValues[1]] = {};
vesselprimary[arrayValues[1]][arrayValues[2]] = value2;
} else {
vesselprimary[arrayValues[1]] = value2;
}
} else {
//Create the VesselSecondary JSON object to put into the body of the post
if (arrayValues.count > 2) {
vesselsecondary[arrayValues[1]] = {};
vesselsecondary[arrayValues[1]][arrayValues[2]] = value2;
} else {
vesselsecondary[arrayValues[1]] = value2;
}
}
});
//console.log(vesselprimary);
this.push(vesselprimary);
}, jsonArr);
But my output is a less than impressive....
"[[],[],[],[],[],[],[],[],[]]"