Search code examples
angularjsjsonrestangular

JSON from flask


I'm trying to fetch data from an url generated with flask not from me with angularjs and restangular. I have this json data:

{"patients": ["{\"_id\": {\"$oid\": \"5677d634cc18e44063fa3556\"}, \"id_patient\": \"2015120001\", \"last_name\": \"Chiacchiaretta\", \"first_name\": \"Piero\", \"gender\": \"M\", \"birthdate\": {\"$date\": 307238400000}, \"birthplace\": \"PESCARA\", \"codice_fiscale\": \"CHCPRI79P27G482U\", \"address\": \"Via Aldo Moro, 114\", \"city\": \"SAN GIOVANNI TEATINO\", \"province\": \"CH\", \"cap\": \"66020\", \"phone_number\": \"3294946261\", \"email\": \"piero.chiacchiaretta@gmail.com\", \"age\": 36, \"nationality\": \"italiana\", \"status\": \"Attivo\"}", "{\"_id\": {\"$oid\": \"5677d634cc18e44063fa3557\"}, \"id_patient\": \"2015120002\", \"last_name\": \"Guidotti\", \"first_name\": \"Andrea\", \"gender\": \"M\", \"birthdate\": {\"$date\": 418568880000}, \"birthplace\": \"AGRIGENTO\", \"codice_fiscale\": \"GDTNDR83D07A089P\", \"address\": \"Via della Liberazione 55\", \"city\": \"SAN BENEDETTO DEL TRONTO\", \"province\": \"AP\", \"cap\": \"63074\", \"phone_number\": \"3404751719\", \"email\": \"rob.guidotti@gmail.com\", \"age\": 32, \"nationality\": \"italiana\", \"status\": \"Revocato\", \"status_note\": \"Problemi con il medico curante!\"}"]}

I don't know what it means ... I'm able to get complete object but I can't access to single value/fields... any suggestion?


Solution

  • Here is how you can decode your response which is an object containing an Array with only one item.

    function ctrl($scope){
      var data = {
    	"patients" : ["{\"_id\": {\"$oid\": \"5677d634cc18e44063fa3556\"}, \"id_patient\": \"2015120001\", \"last_name\": \"Chiacchiaretta\", \"first_name\": \"Piero\", \"gender\": \"M\", \"birthdate\": {\"$date\": 307238400000}, \"birthplace\": \"PESCARA\", \"codice_fiscale\": \"CHCPRI79P27G482U\", \"address\": \"Via Aldo Moro, 114\", \"city\": \"SAN GIOVANNI TEATINO\", \"province\": \"CH\", \"cap\": \"66020\", \"phone_number\": \"3294946261\", \"email\": \"piero.chiacchiaretta@gmail.com\", \"age\": 36, \"nationality\": \"italiana\", \"status\": \"Attivo\"}", "{\"_id\": {\"$oid\": \"5677d634cc18e44063fa3557\"}, \"id_patient\": \"2015120002\", \"last_name\": \"Guidotti\", \"first_name\": \"Andrea\", \"gender\": \"M\", \"birthdate\": {\"$date\": 418568880000}, \"birthplace\": \"AGRIGENTO\", \"codice_fiscale\": \"GDTNDR83D07A089P\", \"address\": \"Via della Liberazione 55\", \"city\": \"SAN BENEDETTO DEL TRONTO\", \"province\": \"AP\", \"cap\": \"63074\", \"phone_number\": \"3404751719\", \"email\": \"rob.guidotti@gmail.com\", \"age\": 32, \"nationality\": \"italiana\", \"status\": \"Revocato\", \"status_note\": \"Problemi con il medico curante!\"}"]
         };
         $scope.patient = angular.fromJson(data.patients[0]);
    }
    <html ng-app>
    <head>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    </head>
    <body ng-controller="ctrl">
      <pre>patient.codice_fiscale = {{patient.codice_fiscale | json}}</pre>
      <pre>patient = {{patient | json}}</pre>
    </body>
    </html>

    Executing the snippet you can see how can be accessed the "codice_fiscale" field of the patient.