I have the following .json
file, which have some lists like values in some elements:
{
"paciente": [
{
"id": 1234,
"nombre": "Pablo",
"sesion": [
{
"id": 12345,
"juego": [
{
"nombre": "bonzo",
"nivel": [
{
"id": 1234,
"nombre": "caida libre"
}
],
"___léeme___": "El array 'iteraciones' contiene las vitorias o derrotas con el tiempo en segundos de cada iteración",
"iteraciones": [
{
"victoria": true,
"tiempo": 120
},
{
"victoria": false,
"tiempo": 232
}
]
}
],
"segmento": [
{
"id": 12345,
"nombre": "Hombro",
"movimiento": [
{
"id": 12,
"nombre": "flexion",
"metricas": [
{
"min": 12,
"max": 34,
"media": 23,
"moda": 20
}
]
}
]
}
]
}
]
},
{
"id": 156,
"nombre": "Bernardo",
"sesion": [
{
"id": 456,
"juego": [
{
"nombre": "Rita",
"nivel": [
{
"id": 1,
"nombre": "NAVEGANDO"
}
],
"___léeme___": "El array 'iteraciones' contiene las vitorias o derrotas con el tiempo en segundos de cada iteración",
"iteraciones": [
{
"victoria": true,
"tiempo": 120
},
{
"victoria": false,
"tiempo": 232
}
]
}
],
"segmento": [
{
"id": 12345,
"nombre": "Escapula",
"movimiento": [
{
"id": 12,
"nombre": "Protracción",
"metricas": [
{
"min": 12,
"max": 34,
"media": 23,
"moda": 20
}
]
}
]
}
]
}
]
}
]
}
From my script, I want to go through it's different nested elements for get specific information
import json
with open('myfile.json') as data_file:
data = json.loads(data_file.read())
patient_id = data["paciente"][0]["id"]
patient_name = data["paciente"][0]["nombre"]
id_session = data["paciente"][0]["sesion"][0]["id"]
game_session = data["paciente"][0]["sesion"][0]["juego"][0]["nombre"]
level_game = data["paciente"][0]["sesion"][0]["juego"][0]["nivel"][0]["nombre"]
iterations = data["paciente"][0]["sesion"][0]["juego"][0]["iteraciones"]
iterations_victory = data["paciente"][0]["sesion"][0]["juego"][0]["iteraciones"][0]["victoria"]
iterations_time = data["paciente"][0]["sesion"][0]["juego"][0]["iteraciones"][0]["tiempo"]
iterations_victory1 = data["paciente"][0]["sesion"][0]["juego"][0]["iteraciones"][1]["victoria"]
iterations_time1 = data["paciente"][0]["sesion"][0]["juego"][0]["iteraciones"][1]["tiempo"]
segment = data["paciente"][0]["sesion"][0]["segmento"][0]["nombre"]
movement = data["paciente"][0]["sesion"][0]["segmento"][0]["movimiento"][0]["nombre"]
#metrics = data["paciente"][0]["sesion"][0]["segmento"][0]["movimiento"][0]["metricas"]
metric_min = data["paciente"][0]["sesion"][0]["segmento"][0]["movimiento"][0]["metricas"][0]["min"]
metric_max = data["paciente"][0]["sesion"][0]["segmento"][0]["movimiento"][0]["metricas"][0]["max"]
metric_average = data["paciente"][0]["sesion"][0]["segmento"][0]["movimiento"][0]["metricas"][0]["media"]
metric_moda = data["paciente"][0]["sesion"][0]["segmento"][0]["movimiento"][0]["metricas"][0]["moda"]
print(
'Patient ID:', patient_id,'\n',
'Patient Name:', patient_name, '\n',
'Session:','\n',
' Id Session:',id_session,'\n',
' Game:', game_session, '\n',
' Level:', level_game, '\n',
' Iterations:', len(iterations),'\n',
' Victory:', iterations_victory, '\n',
' Time:', iterations_time, '\n',
' Victory:', iterations_victory1, '\n',
' Time:', iterations_time1, '\n',
' Affected Segment:', segment, '\n',
' Movement:', movement, '\n',
' Metrics:','\n',
' Minimum:', metric_min, '\n'
' Maximum:', metric_max, '\n'
' Average:', metric_average, '\n'
' Moda/Trend:', metric_moda, '\n'
)
This is my output:
Patient ID: 1234
Patient Name: Pablo
Session:
Id Session: 12345
Game: bonzo
Level: caida libre
Iterations: 2
Victory: True
Time: 120
Victory: False
Time: 232
Affected Segment: Hombro
Movement: flexion
Metrics:
Minimum: 12
Maximum: 34
Average: 23
Moda/Trend: 20
[Finished in 0.0s]
Is it possible to optimize this code? How to can I make this code more readable or short?
I would like especially when I will have query for more of one element (just in case of that exist) in the lists/arrays like as segment, movement, iterations, games, etc
Any orientation is welcome.