преобразование плоского JSON в родительскую дочернюю реляционную структуру из другого JSON - PullRequest
1 голос
/ 21 мая 2019

У меня есть JSON с плоской структурой, и я хочу сделать его родительской дочерней реляционной структурой.

Моя структура JSON выглядит как 1-я точка, если существует какая-либо подпункт, она добавляется к 1-й точкет.е. где бы ни находился подпункт, он добавляется к предыдущей точке

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:

...