Python Forum
Merge all json files in folder after filtering
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Merge all json files in folder after filtering
#1
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:"
Reply
#2
what have you tried so far?
please show code working or not.
Reply
#3
(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.
Reply
#4
it's child toy for you boss.
@deanhystad
Yoriz write Sep-16-2022, 09:37 PM:
Please don't try and provoke people to do the code for you by saying it would be easy for them.
Reply
#5
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
metulburr likes this post
Reply
#6
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.
deneme2 likes this post
Reply
#7
@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!!!
Yoriz write Sep-17-2022, 11:53 AM:
Swearing at me won't provoke me into solving your problems.
You've just moved another step closer to a ban.
Reply
#8
@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.
buran likes this post
Reply
#9
sorry all guys.
i ll not repeat that.
Reply
#10
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.
deneme2 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 532 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  merge all xlsb files into csv mg24 0 332 Nov-13-2023, 08:25 AM
Last Post: mg24
  Rename files in a folder named using windows explorer hitoxman 3 735 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  Rename all files in a folder hitoxman 9 1,482 Jun-30-2023, 12:19 AM
Last Post: Pedroski55
  merge two csv files into output.csv using Subprocess mg24 9 1,758 Dec-11-2022, 09:58 PM
Last Post: Larz60+
  How to loop through all excel files and sheets in folder jadelola 1 4,461 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  python gzip all files from a folder mg24 3 3,985 Oct-28-2022, 03:59 PM
Last Post: mg24
  delete all files and subdirectory from a main folder mg24 7 1,586 Oct-28-2022, 07:55 AM
Last Post: ibreeden
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 5 1,585 Aug-28-2022, 07:11 AM
Last Post: Melcu54
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,475 Dec-18-2021, 09:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020