Home > Enterprise >  Creating a custom json from existing json
Creating a custom json from existing json

Time:09-30

I have Json something like below

Input Json

  "blocks": [
            {
                "key": "",
                "text": "Contents",
                "type": "unstyled",
                "depth": 0,
                "inlineStyleRanges": [
                    {
                        "offset": 0,
                        "length": 8,
                        "style": "BOLD"
                    }
                ],
                "entityRanges": [],
                "data": {}
            },
            {
                "key": "",
                "text": "1.\u00a0\u00a0\u00a0\u00a0 Completed\n Activities & Accomplishments\u00a0 1",
                "type": "unstyled",
                "depth": 0,
                "inlineStyleRanges": [],
                "entityRanges": [
                    {
                        "offset": 0,
                        "length": 49,
                        "key": 0
                    }
                ],
                "data": {}
            },
            {
                "key": "",
                "text": "2.\u00a0\u00a0\u00a0\u00a0 Planned Activities for\n Next Reporting Period\u00a0 3",
                "type": "unstyled",
                "depth": 0,
                "inlineStyleRanges": [],
                "entityRanges": [
                    {
                        "offset": 0,
                        "length": 55,
                        "key": 1
                    }
                ],
                "data": {}
            },

I am trying to extract "text" key data and want to convert it into new json in the key value format I am able to extract text properly now I just wanna know how to separate numeric from letters successfully

def jsontojson():
    with open('C:\Extraction\Docs\TMS TO 692M15-22-F-00073 Monthly Status _ Financial Report July 2022.json') as json_file:
        # json1=json_file.read()
        json1=json.load(json_file)

        value=""
        for dict1 in json1["blocks"]:
            # print(dict1)
            
            for key in dict1:
                
                if key=="text":
                    value =dict1[key]
                    dict2={}
                    d=value.split()
                    print("Value of d",d)
                    if str(d).isdigit():
                        dict2['key1']=d
                    else:
                        dict2['desciption']=d

                    

                    print("Dictionary is",dict2['key'])

For the above code it gives me Key error : key1

Let me know where i am wrong or what I need to do so that i can get the Output

Expected OUTPUT

[
      {
        "key": "",
        "text": "Contents"
    },
    {
        "key": "1.",
        "text": "Completed Activities & Accomplishments"
    },
    {
        "key": "2.",
        "text": "Planned Activities for Next Reporting Period"

    },
    ]

CodePudding user response:

Try (json1 contains the data from your question):

import re

out = []
for d in json1["blocks"]:
    num = re.search(r"^(\d \.)\s*(.*)", d["text"].replace("\n", ""))

    out.append(
        {
            "key": num.group(1) if num else "",
            "text": (num.group(2) if num else d["text"]).rsplit(maxsplit=1)[0],
        }
    )

print(out)

Prints:

[
    {"key": "", "text": "Contents"},
    {"key": "1.", "text": "Completed Activities & Accomplishments"},
    {"key": "2.", "text": "Planned Activities for Next Reporting Period"},
]

CodePudding user response:

Another and easy way is (if your first list is called "Mylist" :

new_list =[]

for dic in Mylist:
    new_list.append({'key':dic['key'],
                     'text':dic['text']})

  • Related