First, let me propose an input format for you. which could be like:
var kenyaConstitutionArray = ["1#SOVEREIGNTY OF CONSTITUTION", "1:1#All sovereign...", "1:2#...",....,"100#....","100:1#..."]
Where only 1#
represents chapter, 1:1#
represents first sub-clause of chapter 1, and 1:1:1#
represents first sub-sub-clause of chapter 1. I've used #
because i assume it will not appear in the text.
To get chapters and clauses, you need to do the following:
var path = text.substr(0, text.indexOf('#'));//it will give path or levels
Here, text is element of array. E.g., text = kenyaConstitutionArray[1]
Now, you have to get chapter:
var chapter = path.substr(0, path.indexOf(':'));
Get sub-clauses in the same way, with little modifications,
And, build json either in the loop or recursively.
Other way is to:
for input, you can use nested arrays as-well. like:
var kenyaConstitution = [["chapter1",[["clause1", [["subclause1"], ["subclause2"]]],["clause2",[["subclause2-1"], ["subclause2-2"]]]]]];
Converting above nested array to json will be very easy for you. In this case, good way would be using recursion.
EDIT:
Complete Code:
[Ignore comments in the code.]
<!DOCTYPE html>
<head>
<title>JSON</title>
<script>
function kenyaConstitutionToJSON() {
var kenyaConstitution = [["chapter1",[["clause1", [["subclause1"], ["subclause2"]]],["clause2",[["subclause2-1"], ["subclause2-2"]]]]]];
var kenyaChapterJSON;
var kenJSON = {};
kenJSON["name"] = "Constitution of Kenya";
kenJSON["children"] = [];
if(kenyaConstitution.length === 0) {
console.log("Constitution is empty! Please pass constitution through Parliament...");
return;
} else {
for (var chapter in kenyaConstitution) { //for each chapter return json
kenyaChapterJSON = convertToJSON(kenyaConstitution[chapter]) || {};
kenJSON["children"].push(kenyaChapterJSON);
}
}
return kenJSON;
}
function convertToJSON(constitutionArray) {
var obj = {};
//constitutionArray[item] = ["chapter1",[["clause1", [["subclause1"], ["subclause2"]]],["clause2",[["subclause2-1"], ["subclause2-2"]]]]]
obj["name"] = constitutionArray[0]; // {name: "children1", children=[ ]}
obj["children"] = [];
//if(constitutionArray.length > 0) {
for (var cl in constitutionArray[1]) {
var kenJSON1 = convertToJSON(constitutionArray[1][cl]);
obj["children"].push(kenJSON1);
}
//} else {
//obj["children"].push(constitutionArray[0]);
//}
return obj;
}
kenyaConstitutionToJSON();
</script>
</head>
<body>
</body>
Place breakpoint on return kenJSON;
line and see the output. It'd be like:
OUTPUT:
{
"name":"Constitution of Kenya",
"children":[
{
"name":"chapter1",
"children":[
{
"name":"clause1",
"children":[
{
"name":"subclause1",
"children":[
]
},
{
"name":"subclause2",
"children":[
]
}
]
},
{
"name":"clause2",
"children":[
{
"name":"subclause2-1",
"children":[
]
},
{
"name":"subclause2-2",
"children":[
]
}
]
}
]
}
]
}
Hope that'd help.