Showing posts with label json. Show all posts
Showing posts with label json. Show all posts

Sunday, 6 September 2020

How to filter JSON data to get value based on the supplied key?

 var existingData = [];


some-loop{

    existingData.push(
{
'email' : $(this).attr("ows_Email_x0020_ID"), //this will be the key
'id' : $(this).attr("ows_ID") // record id for a given email will be fetched
}
}


function getIDbyEmail(emailID)
{
try
{
//var obj; = existingData.filter(i=>i.email==emailID);

var obj = existingData.filter(function(item){
    return item.email == emailID;
});

var _id = obj[0].id;

if(_id == undefined || _id == "" || _id == null)
{
return 0;
}
else
{
return _id;
}
}
catch(err)
{
return 0;
}
}





Wednesday, 27 February 2019

JSON to Excel : How to export SharePoint list data into excel using JavaScript?


<script type="text/javascript" src="/SiteAssets/scripts/grid/jquery-2.2.4.min.js"></script>
<script type="text/javascript" src="/SiteAssets/scripts/grid/xlsx.core.min.js"></script>
<script type="text/javascript" src="/SiteAssets/scripts/grid/FileSaver.js"></script>
<script type="text/javascript" src="/SiteAssets/scripts/grid/jhxlsx.js"></script>


$(document).ready(function(){
loadData();
});


var exportData=[[{"text":"Resource Name"},{"text":"Resource Email"},{"text":"Status1"},{"text":"Status2"},{"text":"Supervisor Email"},{"text":"Career Level"},{"text":"Current Work Location"},{"text":"Project"},{"text":"Enterprise ID"},{"text":"Start Date"}]];

function jsonToExcel()
{

var tabularData = [{
    "sheetName": "All Resources",
    "data": exportData
}];

var options = {
    fileName: "All Resources"
};
Jhxlsx.export(tabularData, options);

}

function loadData()
{//start loadData

$().SPServices({//sart service call
    operation: "GetListItems",
    async: false,
    listName: "Resource List",
    CAMLViewFields: "<ViewFields Properties='True' />",
    CAMLQuery: "<Query><Where><Neq><FieldRef Name='ID' /><Value Type='Counter'>0</Value></Neq></Where><OrderBy><FieldRef Name='Column1' Ascending='True' /></OrderBy></Query>",
    CAMLRowLimit: 0,
    completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {
 
      exportData.push(
      [
         {"text": $(this).attr("ows_Title")},
         {"text": $(this).attr("ows_emailaddress")==undefined?"-":($(this).attr("ows_emailaddress"))},
{"text": $(this).attr("ows_status1")==undefined?"-":($(this).attr("ows_status1"))},
{"text": $(this).attr("ows_Status2")==undefined?"-":($(this).attr("ows_Status2"))},
{"text": $(this).attr("ows_SupervisorEmailID")==undefined?"-":($(this).attr("ows_SupervisorEmailID"))},
{"text": $(this).attr("ows_career_x0020_Level")==undefined?"-":($(this).attr("ows_career_x0020_Level"))},
{"text": $(this).attr("ows_Curr_Loc")==undefined?"-":($(this).attr("ows_Curr_Loc"))},
{"text": $(this).attr("ows_Project")==undefined?"-":($(this).attr("ows_Project"))},
{"text": $(this).attr("ows_Employee_x0020_ID")==undefined?"-":($(this).attr("ows_Employee_x0020_ID"))},
{"text": $(this).attr("ows_Start_x0020_Date")==undefined?"-":($(this).attr("ows_Start_x0020_Date"))}
]
       );
     
      });//end loop
    }

  });//end service call

}//end loadData


Reference: https://www.jqueryscript.net/other/JavaScript-JSON-Data-Excel-XLSX.html

Thursday, 14 February 2019

Excel to JSON : How to convert Excel data into JSON object using JavaScript?

<html>
<head>

<script type="text/javascript" src="./scripts/jquery-2.2.4.min.js"></script>
<script type="text/javascript" src="./scripts/jszip.js"></script>
<script type="text/javascript" src="./scripts/shim.min.js"></script>
<script type="text/javascript" src="./scripts/xlsx.js"></script>

<script language="javascript" type="text/javascript">
var _json_array;
$(document).ready(function(){
    $("#uploadingText").css("display", "none");
    $(function() {
        _fileIn = document.getElementById('fileToUpload');
        if(_fileIn.addEventListener) {
            _fileIn.addEventListener('change', readAllRows, false);//false: bubbling; true: capturing
        }
    });
});


function readAllRows(_event)
{
    try{
        var _file = _event.target.files[0];
        var _reader = new FileReader();
        _reader.onload = function(e) {
       
          var _data = e.target.result;
          var _workbook = XLSX.read(_data, {
            type: 'array',cellDates: true
          });
   
          _workbook.SheetNames.forEach(function(sheetName) {
            var _xl_row = XLSX.utils.sheet_to_json(_workbook.Sheets[sheetName]);
            _json_array = JSON.stringify(_xl_row); //last sheet
          })
        };
   
        _reader.onerror = function(ex) {
          alert('Something went wrong. Please try again after sometime.');
        };
       
        _reader.onloadend = function(ex) {
          uploadData();
        };
       
        _reader.readAsArrayBuffer(_file);//,{cellDates:true,cellText:false});  
    }
    catch(err)
    {
        alert('Invalid workbook.');
    }
}

function uploadData()
{
    jasonData = $.parseJSON(_json_array);
    console.log(jasonData[i]["ColumnName"])
}

</script>
</head>
<body>
    <input style="font-family:'Segoe UI';" type="file" id="fileToUpload" />
</body>
</html>


Note: Based on the timezone you may have to add a day to the date.