Disclaimer: i am a newbie in object oriented Python.
My end goal is to extract data from many JSON files one at a time that have the same structure and to calculate something new from the data and to store all this in a MySQL database.
How do i get the data out of the JSON files and in to a custom object structure in Python?
I need to do this because the stats need to be recalculated with each consecutive JSON file that is read and the contents of the objects may vary based on the accumulated data.
The general idea is that every JSON is a soccer game object which is played between two teams.
A "game" object contains two "team" objects, the length of the match, number of spectators and the date of the game.
A "team" object contains the teams overall score (varies with each game the team plays), a list of "penalty" objects and "goal" that may or may not occur in the JSON and a list of "player" objects, some of which are in the starting line-up list.
A "player" is an object which has a unique name-surname combination, a number and refers to both "penalty" and "goal" objects and the attributes those objects have.
The problems begin when "penalty" type objects refer to the "player" objects only by their number and add information to both "team" and "player" type objects.
- I have thought about using dictionaries, but to my understanding that will not work because of the way players are linked together with penalties and goals.
- I have thought about retrieving values one by one, but that would require some really hard to debug control flow type code and there would be difficulties with the way objects refer to each other again.
- I have looked in to the marshmallow library but since in a newbie to OOP with Python, i am still struggling with understanding the workings of simple examples.
An example of a JSON string that will be passed to the script:
{"Game": {
"Time": "2017/01/11",
"Spectators": 6740,
"Location": "Newlands Stadium",
"T": [
{
"Surname": "Antamo",
"Name": "Dennis"
},
{
"Surname": "Prompa",
"Name": "Pedro"
}
],
"Team": [
{
"TeamName": "Barcelona",
"Players": {"Player": [
{
"Role": "V",
"Nr": 16,
"Surname": "Sam",
"Name": "Sidney"
},
{
"Role": "A",
"Nr": 17,
"Surname": "Cisovsky",
"Name": "Marian"
},
{
"Role": "U",
"Nr": 24,
"Surname": "Verratti",
"Name": "Marco"
},
{
"Role": "A",
"Nr": 27,
"Surname": "Hummels",
"Name": "Mats"
},
{
"Role": "U",
"Nr": 33,
"Surname": "Quintero",
"Name": "Juan"
},
{
"Role": "U",
"Nr": 37,
"Surname": "Díaz",
"Name": "Marcelo"
},
{
"Role": "A",
"Nr": 39,
"Surname": "Criscito",
"Name": "Domenico"
},
{
"Role": "A",
"Nr": 55,
"Surname": "Filip",
"Name": "Lucian"
},
{
"Role": "V",
"Nr": 56,
"Surname": "Saviola",
"Name": "Javier"
},
{
"Role": "A",
"Nr": 64,
"Surname": "Streller",
"Name": "Marco"
},
{
"Role": "U",
"Nr": 73,
"Surname": "Weidenfeller",
"Name": "Roman"
},
{
"Role": "A",
"Nr": 75,
"Surname": "Amelia",
"Name": "Marco"
},
{
"Role": "A",
"Nr": 84,
"Surname": "Sandro",
"Name": "Alex"
},
{
"Role": "U",
"Nr": 89,
"Surname": "Djuricic",
"Name": "Filip"
},
{
"Role": "A",
"Nr": 94,
"Surname": "Varela",
"Name": "Silvestre"
},
{
"Role": "A",
"Nr": 96,
"Surname": "Sauro",
"Name": "Gastón"
}
]},
"StartingLineUp": {"Player": [
{"Nr": 37},
{"Nr": 75},
{"Nr": 73},
{"Nr": 16},
{"Nr": 24},
{"Nr": 96},
{"Nr": 55},
{"Nr": 89},
{"Nr": 27}
]},
"Goals": {"VG": [
{
"Time": "06:09",
"P": [
{"Nr": 96},
{"Nr": 55}
],
"Nr": 24,
"ShotOnGoal": "N"
},
{
"Time": "11:07",
"P": [
{"Nr": 24},
{"Nr": 37},
{"Nr": 16}
],
"Nr": 73,
"ShotOnGoal": "N"
}
]},
"PlayerChanges": {"PlayerChange": {
"Time": "23:34",
"Nr1": 16,
"Nr2": 64
}},
"Penalties": {"Penalty": [
{
"Time": "41:40",
"Nr": 27
},
{
"Time": "56:24",
"Nr": 96
}
]}
},
{
"TeamName": "Skolmeistari",
"Players": {"Player": [
{
"Role": "A",
"Nr": 9,
"Surname": "Debess",
"Name": "Nils"
},
{
"Role": "A",
"Nr": 21,
"Surname": "Zibens",
"Name": "Vilhelms"
},
{
"Role": "V",
"Nr": 22,
"Surname": "Rausis",
"Name": "Vidmants"
},
{
"Role": "A",
"Nr": 25,
"Surname": "Zibens",
"Name": "Janka"
},
{
"Role": "U",
"Nr": 28,
"Surname": "Palodze",
"Name": "Rihards"
},
{
"Role": "A",
"Nr": 30,
"Surname": "Paraugs",
"Name": "Ciltvairis"
},
{
"Role": "A",
"Nr": 33,
"Surname": "Antena",
"Name": "Bo"
},
{
"Role": "U",
"Nr": 34,
"Surname": "Grants",
"Name": "Linards"
},
{
"Role": "U",
"Nr": 36,
"Surname": "Rokturis",
"Name": "Francis"
},
{
"Role": "A",
"Nr": 39,
"Surname": "Svilpe",
"Name": "Einars"
},
{
"Role": "V",
"Nr": 41,
"Surname": "Zirnis",
"Name": "Anderss"
},
{
"Role": "A",
"Nr": 44,
"Surname": "Kompass",
"Name": "Rolands"
},
{
"Role": "U",
"Nr": 47,
"Surname": "Kabacis",
"Name": "Kens"
}
]},
"StartingLineUp": {"Player": [
{"Nr": 9},
{"Nr": 36},
{"Nr": 21},
{"Nr": 39},
{"Nr": 25},
{"Nr": 34},
{"Nr": 30},
{"Nr": 28},
{"Nr": 47}
]},
"Goals": {"VG": [
{
"Time": "20:04",
"P": [
{"Nr": 9},
{"Nr": 25}
],
"Nr": 47,
"ShotOnGoal": "N"
},
{
"Time": "35:38",
"P": [
{"Nr": 30},
{"Nr": 28},
{"Nr": 44}
],
"Nr": 34,
"ShotOnGoal": "N"
},
{
"Time": "63:21",
"P": {"Nr": 34},
"Nr": 39,
"ShotOnGoal": "N"
}
]},
"PlayerChanges": {"PlayerChange": {
"Time": "29:24",
"Nr1": 25,
"Nr2": 44
}},
"Penalties": {"Penalty": {
"Time": "50:57",
"Nr": 36
}}
}
],
"VT": {
"Surname": "Rausis",
"Name": "Rihards"
}
}}
Edit: wording and formatting