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

13 views
Skip to first unread message

Bernardo Garcia

unread,
Oct 10, 2016, 6:54:36 PM10/10/16
to Django users

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:


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








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



Reply all
Reply to author
Forward
0 new messages