Home > front end >  Need to iterate through json and add new json entries using python
Need to iterate through json and add new json entries using python

Time:08-20

So I need to be able to iterate through a json file, looking for entries to add to new entries. This is mostly so I can update a json file between games or when they update. Normally I am good at this, but these json files are formatted in a way I am not used to.

Example json:

[
    {
        "Category": "Food",
        "Types": [
            888,
            784654,
            484,
            44841
        ]
    },
    {
        "Category": "",
        "Types": [
            4854
        ]
    }
]

I want to take every number from Food and other named Category's and not the empty categories and create a new entry at the root of the json with the numbers

New entry:

"Category": "",
"Types": [
    888
]

Final code example:

{
    "Category": "Food",
    "Types": [
        888,
        784654,
        484,
        44841
    ]
},
{
    "Category": "",
    "Types": [
        4854
    ]
},
{
    "Category": "",
    "Types": [
        888
    ]
},
{
    "Category": "",
    "Types": [
        784654
    ]
},
{
    "Category": "",
    "Types": [
        484
    ]
},
{
    "Category": "",
    "Types": [
        44841
    ]
}

CodePudding user response:

This takes your original and creates your "final" example...

import json

with open(".\\start.json", "r", encoding="utf-8") as jf:
    the_list: list[dict] = json.load(fp=jf)
new_obj_list = []
for o in the_list:
    if o["Category"].strip() != "":
        for t in o["Types"]:
            new_obj_list.append({
                "Category": "",
                "Types": [t]
            })
the_list.extend(new_obj_list)
with open(".\\output.json", "w", encoding="utf-8") as jf:
    json.dump(fp=jf, obj=the_list, indent=4)

start.json:

[
    {
        "Category": "Food",
        "Types": [
            888,
            784654,
            484,
            44841
        ]
    },
    {
        "Category": "",
        "Types": [
            4854
        ]
    }
]

output.json:

[
    {
        "Category": "Food",
        "Types": [
            888,
            784654,
            484,
            44841
        ]
    },
    {
        "Category": "",
        "Types": [
            4854
        ]
    },
    {
        "Category": "",
        "Types": [
            888
        ]
    },
    {
        "Category": "",
        "Types": [
            784654
        ]
    },
    {
        "Category": "",
        "Types": [
            484
        ]
    },
    {
        "Category": "",
        "Types": [
            44841
        ]
    }
]

CodePudding user response:

You could do it like this:

import json

JDATA = '''[
    {
        "Category": "Food",
        "Types": [
            888,
            784654,
            484,
            44841
        ]
    },
    {
        "Category": "",
        "Types": [
            4854
        ]
    }
]'''

pd = json.loads(JDATA)

for d in pd:
    if d['Category'] != '':
        for v in d['Types']:
            pd.append({'Category': '', 'Types': [v]})

print(json.dumps(pd, indent=4))

Output:

[
    {
        "Category": "Food",
        "Types": [
            888,
            784654,
            484,
            44841
        ]
    },
    {
        "Category": "",
        "Types": [
            4854
        ]
    },
    {
        "Category": "",
        "Types": [
            888
        ]
    },
    {
        "Category": "",
        "Types": [
            784654
        ]
    },
    {
        "Category": "",
        "Types": [
            484
        ]
    },
    {
        "Category": "",
        "Types": [
            44841
        ]
    }
]

CodePudding user response:

Just exclude everything but the rows that have an empty string for the category:

otherjson = []
for row in data:
    if row["Category"] is not "":
        otherjson.append(row)
  • Related