bgarcial bgarcial - 1 month ago 6
JSON Question

How to read the json file of a dinamical way in relation to their size structure

I have the following JSON file named

ProcessedMetrics.json
which is necessary read for send their values to some template:

{
"paciente": {
"id": 1234,
"nombre": "Pablo Andrés Agudelo Marenco",
"sesion": {
"id": 12345,
"juego": [
{
"nombre": "bonzo",
"nivel": [
{
"id": 1234,
"nombre": "caida libre",
"segmento": [
{
"id": 12345,
"nombre": "Hombro",
"movimiento": [
{
"id": 1234,
"nombre": "flexion",
"metricas": [
{
"min": 12,
"max": 34,
"media": 23,
"moda": 20
}
]
}
]
}
],
"___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
}
]
}
]
}
]
}
}
}


Through of the following class based view I am reading a JSON file.

class RehabilitationSessionDetail(LoginRequiredMixin,DetailView):
model = RehabilitationSession
template_name = 'rehabilitationsession_detail.html'

def get_context_data(self, **kwargs):
context=super(RehabilitationSessionDetail, self).get_context_data(**kwargs)
is_auth=False

user = self.request.user
if user.is_authenticated():
is_auth=True

with open('ProcessedMetrics.json') as data_file:
session_data=json.loads(data_file.read())

#Sending a data to template
context.update({'is_auth':is_auth,
'session_data':session_data
})
return context


In my template
rehabilitationsession_detail.html
I put my tag of this way:

<td>{{session_data.paciente.sesion.juego}}</td>


Then I get the document json in my template

enter image description here

In my template, I want get the dictionary(before json document) values of a separate way such as follow:

enter image description here

The idea is that without matter the nested levels of the json document I can get the values. Sometimes, the json document will have more identation levels in their structure and other times will be a json document more simple

I would that independently of the json document size (if this have more than one item in your arrays) will be possible read and get all the values.

I try accessing to the specific item from the
RehabilitationSessionDetail
view of this way:

segment = data["paciente"]["sesion"]["juego"][0]["nivel"][0]["segmento"][0]["nombre"]


And this works, but not always I will get the same json document structure.

In summary, How to can I get the values (nested and parents) of my json document for send them to the template?

I hope can be clear in my question. Any orientation is highly graceful

Answer

If I understand correctly from your question, with different JSON sizes you mean different array sizes? You can loop in the django template to get all the information out. So you would do for a nested visualisation:

{% for nest1 in data["patiente"]["sesion"]["juego"] %}
<li>{{ nest1["nombre"] }}</li>
{% for nest2 in nest2["nivel"] %}
etc...
{% endfor %}
{% endfor %}

To get it as a table you would seperate the data and make a loop for each table column

<tr>
    {% for nest1 in data["patiente"]["sesion"]["juego"] %}
    <td>{{ nest1["nombre"] }}</td>
    {% endfor %}
</tr>

<tr>
    {% for nest1 in data["patiente"]["sesion"]["juego"] %}
    {% for nest2 in nest2["nivel"] %}
    <td>{{ nest2["relevant_key"] }}</td>
    {% endfor %}
    {% endfor %}
</tr>
etc...

because of the nested json data representation you template code will have to follow this nesting.

Hope I understood your question correctly and hope this helps.

Comments