Python Forum

Full Version: Merge all json files in folder after filtering
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello all,
I'd like to ask your help to merge all json files in one folder after filtering data in each of them, name new file as "merged.json" in that folder.
Many results from various sports included in these json files. But I wanna have only football results together in merged.json file.
You ll see "ID":1 as dictionary of football results.
You can see two example json and desired "merged.json" in the below.

example1.json
{
  "results": [
    {
      "Elems": [
        {
          "Elems": [
            {
              "ligName": "Premier League",
              "nameGame": "Chelsea - Liverpool",
              "score": "2-2"
            },
            {
              "ligName": "Premier League",
              "nameGame": "Arsenal - Leeds",
              "score": "3-1"
            }
          ],
          "Name": "England. League Cup"
        },
        {
          "Elems": [
            {
              "ligName": "La Liga",
              "nameGame": "Barcelona - Valencia",
              "score": "2-0"
            }
          ],
          "Name": "La Liga"
        }
      ],
      "ID": 1
    },
    {
      "ID": 2
    },
    {
      "ID": 3
    },
    {
      "ID": 4
    }
  ]
}
example2.json
{
  "results": [
    {
      "ID": 4
    },
    {
      "Elems": [
        {
          "Elems": [
            {
              "ligName": "Bundesliga",
              "nameGame": "Bayern - Dortmund",
              "score": "3-0"
            }
          ],
          "Name": "Bundesliga"
        },
        {
          "Elems": [
            {
              "ligName": "Ligue 1",
              "nameGame": "PSG - Monaco",
              "score": "4-0"
            }
          ],
          "Name": "Ligue 1"
        },
        {
          "Elems": [
            {
              "ligName": "Serie A",
              "nameGame": "Inter - Juventus",
              "score": "1-1"
            }
          ],
          "Name": "Serie A"
        }
      ],
      "ID": 1
    },
    {
      "ID": 3
    },
    {
      "ID": 2
    }
  ]
}
merged.json
{
  "results": [
    {
      "Elems": [
        {
          "Elems": [
            {
              "ligName": "Premier League",
              "nameGame": "Chelsea - Liverpool",
              "score": "2-2"
            },
            {
              "ligName": "Premier League",
              "nameGame": "Arsenal - Leeds",
              "score": "3-1"
            }
          ],
          "Name": "England. League Cup"
        },
        {
          "Elems": [
            {
              "ligName": "La Liga",
              "nameGame": "Barcelona - Valencia",
              "score": "2-0"
            }
          ],
          "Name": "La Liga"
        },
	{
          "Elems": [
            {
              "ligName": "Bundesliga",
              "nameGame": "Bayern - Dortmund",
              "score": "3-0"
            }
          ],
          "Name": "Bundesliga"
        },
        {
          "Elems": [
            {
              "ligName": "Ligue 1",
              "nameGame": "PSG - Monaco",
              "score": "4-0"
            }
          ],
          "Name": "Ligue 1"
        },
        {
          "Elems": [
            {
              "ligName": "Serie A",
              "nameGame": "Inter - Juventus",
              "score": "1-1"
            }
          ],
          "Name": "Serie A"
        }
      ]
    }
  ]
}
Note: I usually indicate encoding as "with open("example.json", encoding="UTF-8") as f:"
what have you tried so far?
please show code working or not.
(Sep-15-2022, 09:49 PM)Larz60+ Wrote: [ -> ]what have you tried so far?
please show code working or not.

maybe
import json
with open("*.json", encoding="UTF-8") as f:
*.json.find("ID:1,")
*.json.append(*.json)
it's too difficult for me pal.
it's child toy for you boss.
@deanhystad
python-forum.io Wrote:This forum is focused on education. It exists to help people learn Python. We don’t exist to solve others’ problems
Please read Homework and No Effort Questions
When you open a json file, you have a Python dictionary.

Here is some information on merging Python dictionaries.

Python version 3.9 has a merge operator, but, as I understand it, if a key is present in more than one dictionary, the last value for that key will be used.
I use Python 3.8.10, so the merge operator doesn't work for me!

Quote:If a key is found in multiple dicts, the last seen value will be used, with all of the above mentioned methods (meaning the earlier values will be overwritten).

From the link above:

dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}

merged = dict1.copy()
merged.update(dict2)
Sure enough, the final value of b is 3.

If you need all values for a given key and you have the same key in multiple dictionaries, you would need to convert all the values to lists and append values, shouldn't be too difficult.
@Pedroski55 thanks for your response
i asked here only for max 5-10 lines code.
some idiots here made it the biggest problem of the planet.
stop educating me @Yoriz
if you're super enough, find a solution with coding for climate change
bastard!!!
@deneme2 if you seriously want help around here you must follow the forum rules.
Clean up your act or we will have no choice but to ban you.
sorry all guys.
i ll not repeat that.
My thinking is that you are thinking about the problem all wrong.

It think what you really want to do is read in all the json files, load them into a dataframe, filter the dataframe, and write one json file that contains all the filtered results. Great thing is, you already know how to do most of that.

In another thread you learned how to read a tortured json format file into a dataframe using normalize. Thanks snippsat!

In the same thread you learned how to filter out rows boolean arrays and comparison statements. Thanks me!

In another thread I just read today somebody is trying to read a bunch of CSV files into dataframes, concatenate the dataframes, and write the big, concatenated dataframe to another CSV file. Replace "CSV" with "json" and that sounds a lot like what you want to accomplish.

This is a forum, not a free programming service. It only works if you contribute. Eventually your contribution will be answering questions that other people ask. Until then your contribution will have to be asking good questions and following the forum rules.
Pages: 1 2