sam_oda

JSON generator using webviewer

Discussion created by sam_oda on Apr 5, 2018
Latest reply on Apr 6, 2018 by sam_oda

Inspired by this post,

https://community.filemaker.com/message/751266?commentID=751266&et=watches.email.thread#comment-751266

I also made a JSON generator, but took a different approach.

I use a webviewer.

Passing CSV-like data and JSON scheme to webviewer, the webviewer returns JSON.

I attached the DEMO file.

I'd really appreciate it if you could give me your opinion on this.

 

 

Special thanks to @steve_ssh

Thanks for your permission to use this sample data.

 

Screen Shot 2018-04-05 at 13.59.15.png

 

Here is the webviewer code.

 

Let(

code="<!DOCTYPE html>

<html lang='en'>

<head>

  __MOTW__

   <meta charset='UTF-8'>

   <meta http-equiv='X-UA-Compatible' content='IE=edge' >

   <title>FileMaker - GetJSON via WebViewer</title>

   <style>

   *{margin:0;}

   body{width:100vw;height:100vh;overflow:hidden;}

   button{width:100vw;height:100vh;font:24px sans-serif;color:#555}

   button:hover{color:#f55}

   </style>

</head>

<body>

<button id='_btn'>Get JSON</button>

<textarea id='_target'>__RESULT__</textarea>

<script>

   var btn = document.getElementById('_btn');

   var target = document.getElementById('_target');

 

   var scheme=[__SCHEME__];

   var result=target.value.replace(/[\n\u2028\u2029]/g, '\r');

   var json = textToJSON(result, scheme,'__LINEDELIM__','__ITEMDELIM__', '__KEYITEM__');

   target.value = json;

 

   btn.onclick=function(){

        copy();

        location.href='fmp://$/MakeJSON?script=getJSON';

  };

   document.body.onmouseenter = function(){

        location.href='fmp://$/MakeJSON?script=activateWV';

  };

 

   function copy(){

        if(window.clipboardData){/*Windows*/
             clipboardData.setData('TEXT', target.value);

       }else{/*Mac*/
             target.select();

             document.execCommand('copy');

             target.blur();

       }

  }

 

   function textToJSON(text, scheme, lineDelimiter, itemDelimiter, keyItem) {

        var lineDelimiter = lineDelimiter || '__LINEDELIM__';

        var itemDelimiter = itemDelimiter || '__ITEMDELIM__';

        var lines = text.split(lineDelimiter);

        var result;

        if(keyItem){

             result={};

       }else{

             result=[];

       }

        lines.forEach(function(line) {

             var obj = {};

             var values = line.split(itemDelimiter);

             var idKey;

             values.forEach(function(value, index) {

             var key = scheme[index].label;

             var type = scheme[index].type;

             if(key == keyItem){

                  idKey=values[index];

                  return;

            }

             if(type == 2) {

                 value = Number(value);

            }else if(type == 5){

                 value = Boolean(Number(value));

            }

             obj[key] = value;

          });

 

        if(keyItem){

             result[idKey] = obj;

       }else{

             result.push(obj);

       }

  });

   return JSON.stringify(result);

  }

</script>

</body>

</html>

";

Substitute (

code;

[";";";" & Char(10)];/* for easy debug */

["__SCHEME__";MakeJSON::scheme];

["__RESULT__";ExecuteSQL (MakeJSON::sql; "__ITEMDELIM__"; "__LINEDELIM__" ) ];

["__KEYITEM__";MakeJSON::keyItem];

["__MOTW__";

Char(1000013) & "<!-- saved from url=(0014)about:internet -->" & Char(1000013) ]

)

)

Attachments

Outcomes