0 Replies Latest reply: Sep 14, 2016 10:28 AM by Robert Klein RSS

    Merging Sheets from Different Qlik Sense Apps using the Backup and Restore Mashup and a Script?

    Robert Klein

      Stefan Stoichev has developed a great mashup to Backup and Restore Qlik Sense apps in JSON format.

       

      I work on a team where we may have 2-3 developers working at once on a Qlik Sense app with the same data model and would like to be able to then efficiently merge sheets from our different apps into a single app.

       

      Stefan's mashup allows us to manually copy the desired sheets from the “sheets” array in each of our serialized JSON files and paste into a ‘target’ JSON file that we can then use to restore into a Qlik Sense app.

       

      We would like to reduce the chances of human error for larger projects in this process by merging sheets with a script.

       

      We thought we might be able to accomplish that in R (we do not have a lot of experience with JavaScript), for example, with something like this in a Windows environment:

       

      #install.packages(c("rjson", "RJSONIO", "jsonlite"))

       

      library(rjson)

      library(RJSONIO)

      library(jsonlite)

       

      dir <- "C:\\Users\\MyUserName\\Downloads\\"

       

      #Serialized JSON files created with the Backup and Restore Mashup for different Qlik Sense apps

      json1 <- paste0(dir, "TestMergeFromR1.qvf_20160908-150428.json")

      json2 <- paste0(dir, "TestMergeFromR2.qvf_20160908-114847.json")

       

      json_data1 <- rjson::fromJSON(file=json1)

      json_data2 <- rjson::fromJSON(file=json2)

       

      #Save original JSON to a new object

      merged_json <- json_data1

       

      #Merge the "sheets" arrays from both JSON files

      merged_json$sheets <- c(json_data1$sheets, json_data2$sheets)

       

      #Export the merged JSON to then use in a Restore to Qlik Sense app

      setwd(dir)

      sink('r_test_json.json')

      cat(RJSONIO::toJSON(merged_json, pretty=T))

      sink()

       

      We have verified that the JSON that R exports (an example file is attached as well) is valid using http://jsonlint.com/.

       

      However, when we try to use this JSON file to restore to a Qlik Sense app, we get the error “Uncaught (in promise) Object {code: -32700, parameter: “Unexpected JSON token”, message: “JSON parse error”} once we click the ‘Restore’ button.  Before that, the JSON file seems to load correctly (the For delete, For insert, For update line below the ‘Choose File’ is updated).

       

      In trying to walk through the steps of backup-and-restore.js (added a console.log line to the original), the function deleteObjects() will successfully print the qType and qId before we get the error:

       

      function deleteObjects() {

          return Promise.all(status.forDelete.map(function(d) {

      console.log('deleteObjects function: ' + d.qType + ', ' + d.qId)

            if (d.qType === 'measure') {

      return main.app.destroyMeasure(d.qId).then(function() {

      return importData.push([d.qType, '', d.qId, 'delete']);

      });

            } else if (d.qType === 'dimension') {

      return main.app.destroyDimension(d.qId).then(function() {

      return importData.push([d.qType, '', d.qId, 'delete']);

      });

            } else if (d.qType === 'snapshot' || d.qType === 'bookmark') {

      return main.app.destoryBookmark(d.qId).then(function() {

      return importData.push([d.qType, '', d.qId, 'delete']);

      });

            } else if (d.qType === 'variable') {

      return main.app.destroyVariableById(d.qId).then(function() {

      return importData.push([d.qType, '', d.qId, 'delete']);

      });

            } else {

      return main.app.destroyObject(d.qId).then(function() {

      return importData.push([d.qType, '', d.qId, 'delete']);

      });

            }

          }));

        }

       

      Is there a solution that we are missing that anyone could help us find?  For instance, assuming we are generally on the right track, would anyone happen to have any suggestions to format JSON in the way the mashup expects?  We noticed there are some small differences, such as R produces empty sets as “[]” (for example, ‘"qThumbnail" : []’), while the serialized JSON uses “{}” (‘"qThumbnail": {}’).