简体   繁体   中英

How do I turn a piece of text into a parent children JSON file?

I have a text file that consists of chapters and clauses. Its the constitution of Kenya .

I want to convert it to something similar to Flare.json which looks like below.

{"name": "ROOT",
 "children": [
        {"name": "Hemiptera",
         "children": [
             {"name": "Miridae",
              "children": [
                  {"name": "Kanakamiris", "children":[]},
                  {"name": "Neophloeobia",
                   "children": [
                       {"name": "incisa", "children":[] }
                   ]}
              ]}
         ]},
        {"name": "Lepidoptera",
         "children": [
             {"name": "Nymphalidae",
              "children": [
                  {"name": "Ephinephile",
                   "children": [
                       {"name": "rawnsleyi", "children":[] }
                   ]}
              ]}
         ]}
    ]}
}

Is there a way I can programatically do this in either Javascript, Python or R?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM