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:

<http://i.stack.imgur.com/SLZB2.png>


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


<http://i.stack.imgur.com/MsyHT.png>







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



-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c5256c63-03e3-42ff-9e05-41b181292ee4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to