将平面JSON从另一个JSON转换为父子关系结构

时间:2019-05-21 16:16:10

标签: python json python-3.x text-processing python-textprocessing

我有一个平面结构的JSON,我想将其作为父子关系结构。

我的JSON结构有些像这样 第一个点(如果存在任何子点)将附加到第一点,即子点在任何位置附加到上一个点

my input json
json format image===>https://i.stack.imgur.com/4xFJp.png

structure
{
  "0": "Points",
  "1": [
    "1. This is the first Point"
  ],
  "2": [
    "2. This is Second point",
    "(a) SubPoint of 2",
    "(b) Sub Point of 2",
    "(c) Sub Point of 2",
    "(d) (i) Sub Point of d",
    "(ii)Sub point of d",
    "(iii) Sub point of d",
    "(iv) Sub point of d",
    "(A) Subpoint of (iv)",
    "(B) Subpoint of (iv)",
    "(C) Subpoint of (iv)",
    "(D) Subpoint of (iv)",
    "(e) Sub Point of 2",
    "(f) Sub Point of 2"
  ]
}

 with open('ks.json','r') as fp:
    fil=fp.read()
    i=0
    for item in fil:
        print(item)
        regex=r"^[0-9]+\.\s.*"
        regex1=r"^\([a-z]\)\s.*"

            j=0
            sub = []
            for i in item:
                if sub:
                    match = re.match(regex, i)
                    match1=re.match(regex1,i)
                    if match:
                         Points[str(j)] = sub
                else:
                    if sub:
                        Points[str(j)] = sub
                    sub = [i]
                    Points[str(j)] = i
expected json output should be in this way

Notes

1. This is the first Point"
2. This is Second point"
    "(a)":["SubPoint of 2"],
    "(b) Sub Point of 2",
    "(c) Sub Point of 2",
    "(d) 
        (i) Sub Point of d",
        "(ii)Sub point of d",
        "(iii) Sub point of d",
        "(iv) Sub point of d",
            "(A) Subpoint of (iv)",
            "(B) Subpoint of (iv)",
            "(C) Subpoint of (iv)",
            "(D) Subpoint of (iv)",
    "(e) Sub Point of 2",
    "(f) Sub Point of 2"

输入json:

0 个答案:

没有答案