-1

It's all in the title, I would like to generate a CSV file from the JSON objet get from this Ajax request,

The JSON I get represent all the recordings from a form :

enter image description here

I already have something that work for one field value a a single recording (the 0 here) :

    <!DOCTYPE html>
    <meta charset="utf-8"/>
    <html>
        <head>
            <title>This is Website Title</title>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js" type="text/javascript"></script>
            <script src="../FileSaver.js"></script>
            <script>
                var formId = 566091
                // Définition des paramètres de la requête HTTP
                var settings = {
                    "async": true,
                    "crossDomain": true,
                    "url": "https://www.kizeoforms.com/rest/v3/forms/"+formId+"/data/readnew",
                    "method": "GET",
                    "headers": {
                            "content-type": "application/json",
                            "Authorization": "******",
                    }
                }

                // Envoi de la requête et affichage en console de la réponse
                $.ajax(settings).done(function (response) {
                    console.log(response);
                    var ssa3 = [];
                    for (var i = 0 ; i <= response.data.length; i++) {
                        ssa3.push(response.data[i].fields.ssa_3_a_22h00.value);
                    }
                    //var ssa3 = response.data[0].fields.ssa_3_a_22h00.value;
                    var blob = new Blob([ssa3], {type: "application/csv;charset=utf-8"});
                    saveAs(blob, "ssa3.csv");
                });
            </script>
        </head>
    </html>

I would now like to have this field value for all the recordings, I have tried to push it into a table but console tells me "i is undefined"

                $.ajax(settings).done(function (response) {
                    console.log(response);
                    var ssa3 = [];
                    for (var i = 0 ; i <= response.data.length; i++) {
                        ssa3.push(response.data[i].fields.ssa_3_a_22h00.value);
                    }
                    var blob = new Blob([ssa3], {type: "application/csv;charset=utf-8"});
                    saveAs(blob, "ssa3.csv");
                });
JS1
  • 71
  • 1
  • 5
  • 3
    May we see an example of the JSON? What have you tried so far? – evolutionxbox Feb 11 '20 at 14:46
  • Hi there, JS1 :-) You could just add the json example of the previous-opposite one: '{\r\n "items": [\r\n "label|label", "test1|test4", "test2|test5", "test3|test6" \r\n ]\r\n}'. Ain't this similar to your initial json here? – María Antignolo Feb 11 '20 at 14:55
  • I think I should add how to put the content into a file and save or download it. what do you intend to do with the result? – María Antignolo Feb 11 '20 at 15:10
  • Refer this: https://stackoverflow.com/questions/8847766/how-to-convert-json-to-csv-format-and-store-in-a-variable – Rijin Mk Feb 11 '20 at 15:15
  • `"content-type": "application/json",` — You're making a GET request. There is no content in the request body to describe the type of. – Quentin Feb 11 '20 at 15:27
  • evolutionxbox, I have updated :my post – JS1 Feb 11 '20 at 15:51
  • mantingablue, yes I would like to eventually generate a separate file – JS1 Feb 11 '20 at 15:52
  • I can now generate a CSV file, and I'm trying to classify data in it, please take a look it should be much clearer for you to understand – JS1 Feb 12 '20 at 15:06

1 Answers1

0

Finally found it, I used a forEach to browse the data :

$.ajax(settings).done(function (response) {

                console.log(response);
                var ronde1n = [];

                //on définit data qu'on va devoir parcourir avec un forEach
                const data = response.data;

                //on envoie les headers du fichiers csv
                ronde1n.push("Numéro d'enregistrement,ID,Date et heure,conso SSA3");

                //on parcours l'ensemble des enregistrements du formulaire Ronde 1 nuit
                  data.forEach(function (i) {
                     //on envoie les valeurs des champs qui nous intéressent pour chaque enregistrement
                     ronde1n.push("\r\n" + i.record_number + "," + i.id + "," + i.fields.date_et_heure.value + "," + i.fields.ssa_3_a_22h00.value);
                  });

                //création du fichier CSV
                var blob = new Blob([ronde1n], {type: "application/csv;charset=utf-8"});
                  saveAs(blob, "ronde1_nuit.csv");

            });
JS1
  • 71
  • 1
  • 5