Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Use uploaded txt file as JSON data in javascript

  • tomschuette
  • tomschuette's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 9 months ago - 2 years 9 months ago #230038 by tomschuette
Please help us help you and fill where relevant:
Your LimeSurvey version: [see right hand bottom of your LimeSurvey admin screen]
Own server or LimeSurvey hosting:
Survey theme/template:
==================
Hi everyone,

i have the following code and instead of having to copy the entire data as text into my code (var myData), I would like to be able to "read" a txt file (in JSON format) that I uploaded to Limesurvey. It has the same structure as the data in the code.

Underneath the "var myData" you can see what I came up with, but that doesn't seem to do the trick.

Thank you for any suggestions as always :)

Code:
<table id="dce_table">
</table>
<script type="text/javascript" charset="utf-8">
  
    $(document).on('ready pjax:scriptcomplete',function(){
 
      var myData = [
            { "Version":"1","Task":"1","Concept":"1","Att1":"Banane","Att2":"5€","Att3":"27,56","Att4":"$$$","Att5":"50°C"},
            { "Version":"1","Task":"1","Concept":"2","Att1":"Karotte","Att2":"10€","Att3":"58,87","Att4":"€€€","Att5":"165°F"},
            { "Version":"1","Task":"1","Concept":"3","Att1":"Vanilleeis","Att2":"10€","Att3":"58,87","Att4":"€€€","Att5":"165°F"},
            { "Version":"1","Task":"2","Concept":"1","Att1":"Birne","Att2":"5€","Att3":"27,56","Att4":"$$$","Att5":"50°C"},
            { "Version":"1","Task":"2","Concept":"2","Att1":"Sellerie","Att2":"10€","Att3":"58,87","Att4":"€€€","Att5":"165°F"},
            { "Version":"1","Task":"2","Concept":"3","Att1":"Obstsalat","Att2":"10€","Att3":"58,87","Att4":"€€€","Att5":"165°F"},
            { "Version":"2","Task":"1","Concept":"1","Att1":"Apfel","Att2":"5€","Att3":"27,56","Att4":"$$$","Att5":"50°C"},
            { "Version":"2","Task":"1","Concept":"2","Att1":"Gurke","Att2":"10€","Att3":"58,87","Att4":"€€€","Att5":"165°F"},
            { "Version":"2","Task":"1","Concept":"3","Att1":"Mousse au Chocolat","Att2":"10€","Att3":"58,87","Att4":"€€€","Att5":"165°F"},
            { "Version":"2","Task":"2","Concept":"1","Att1":"Blaubeeren","Att2":"5€","Att3":"27,56","Att4":"$$$","Att5":"50°C"},
            { "Version":"2","Task":"2","Concept":"2","Att1":"Zucchini","Att2":"10€","Att3":"58,87","Att4":"€€€","Att5":"165°F"},
            { "Version":"2","Task":"2","Concept":"3","Att1":"Erdbeerjoghurt","Att2":"10€","Att3":"58,87","Att4":"€€€","Att5":"165°F"}
        ]
 
// THIS IS WHAT I IMAGINED COULD WORK BUT IT DOESN'T:
          //var surveyID = 123456;
          //var filename = 'path/to/file/filename.txt';
          //var myData = JSON.parse(filename);
 
      var attributes = ["Attribut 1", "Attribut 2", "Attribut 3","Attribut 4", "Attribut 5"];
        var myTASK = "1";
        var alternatives = ["Option 1","Option 2","Option 3"];
        var myVERSION = "2"; // replace with random number (generated on earlier page) later on
        var h = 0;
          var f = 0;
 
 
        // Check number of columns to be created
        var col = ;
        col.push("Option");
        for (var key in myData[0]) {
            if (key != "Version" &amp;&amp; key != "Task" &amp;&amp; key != "Concept") {
                col.push(key);
                }
        }
          col.push("Buttons");
 
 
        var table = document.createElement("table");
 
        var tr = table.insertRow(-1);
 
          // Create table headers
          for (var i = 0; i < col.length; i++) {
          if (i === 0) {
            var th = document.createElement("th");
            th.innerHTML = "Option";
            tr.appendChild(th);
              }
          else if (i < col.length-1) {
            var th = document.createElement("th");
            th.innerHTML = attributes[i-1];
            tr.appendChild(th);
              }
          else {
            var th = document.createElement("th");
            th.innerHTML = "Auswahl";
            tr.appendChild(th);
            }
        }
         // Create rows
        for (var i = 0; i < myData.length; i++) {
            if (myData[i]["Version"] === myVERSION &amp;&amp; myData[i]["Task"] === myTASK) {
 
              tr = table.insertRow(-1);
 
              for (var j = 0; j < col.length; j++) {
 
                if (col[j] === "Buttons") {
                  tr.insertCell(-1);
                }
                else if (j == 0) {
                        var createLabel = tr.insertCell(-1);
                        createLabel.innerHTML = alternatives[f];
                        f += 1;
                      }
                else {
                          var tabCell = tr.insertCell(-1);
                          tabCell.innerHTML = myData[i][col[j]];
                      }
              }
            }
        }
 
        var divContainer = document.getElementById("dce_table");
        divContainer.innerHTML = "";
        divContainer.appendChild(table);
    });[/i][/i][/i]
Code:
[i][i][i]</script>[/i][/i][/i]
Last edit: 2 years 9 months ago by tomschuette. Reason: forgot closing brackets

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 9 months ago #230048 by Joffm
What about
$.getJSON()  method?

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 9 months ago #230096 by Joffm
Use the library "JQuery.csv"

With
Code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-csv/1.0.11/jquery.csv.min.js"></script>
 
<script type="text/javascript" charset="utf-8">
$(document).on('ready pjax:scriptcomplete',function(){
 
        var url = "/upload/surveys/{SID}/files/file.csv";
        var myData;
        $.get(url,function(data){
           myData = $.csv.toObjects(data);
        });
        alert(0); // This is the crucial point
 
        var attributes = ["Attribut 1", "Attribut 2", "Attribut 3","Attribut 4", "Attribut 5", "Attribut 6"];
 // and so on
and the file

Version,Task,Concept,Att1,Att2,Att3,Att4,Att5,Att6
1,1,1,Banane,5,27,56,$$$,50°C,btn
1,1,2,Karotte,6,27,56,$$$,50°C,btn
1,1,3,Vanille,7,27,56,$$$,50°C,btn
1,2,1,Birne,8,27,56,$$$,50°C,btn

I get



But one issue.
You see the "alert(0)".
Without this it doesn't seem to work.
As my knowledge of javascript is really very limited I have no idea.

Nevertheless: Now you have an instriment to read in and convert a text file .

Joffm
 

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 9 months ago #230110 by tpartner

You see the "alert(0)".
Without this it doesn't seem to work.

On my phone so I can't give an extensive answer but this may be caused by the loading time for jquery.csv from a CDN. Try loading that script from your server or using the jQuery getScript() function.

- api.jquery.com/jquery.getscript/

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose