I have to get the data from SharePoint(Online) List and put the data to JSON format and create a JSON file in Local drive or SharePoint list.
I am new to SharePoint online and JSON can anyone help me on this.Give me some code examples.
You can retrieve sharepoint list data with javascript object model (JSOM), the data is returned in JSON format
You can get the data from SharePoint List using REST API. It returns the data into array object and then you can convert into JSON. Refer below code:
//Retrieve list items from sharepoint using REST API
var myJsonString; //To store the data into JSON format
function GetSampleListItems() {
siteURL = _spPageContextInfo.siteAbsoluteUrl;
var apiPath = siteURL + "/_api/lists/getbytitle('samplelist')/items";
$.ajax({
url: apiPath,
headers: {
Accept: "application/json;odata=verbose"
},
async: false,
success: function(data) {
if(data.d.results.length>0)
{
myJsonString = JSON.stringify(data);
}
},
eror: function(data) {
console.log("An error occurred. Please try again.");
}
});
You used the term "JSON file". Normally, the word "file" is used to refer to a physical file on some device (not a string you are dealing with in your code, or a JavaScript object). The browser has no access to physical files on your machine. It cannot read or write them. Actually, the browser does not even really have the notion of a "file". Thus, you cannot just read or write some JSON file on your local machine.
Edit: Create a JSON file using JavaScriptSerializer Class. JavaScriptSerializer class lies in System.Web.Script.Serialization namespace and this namespace found in System.web.Extensions.dll.
So to use JavaScriptSerializer class you must add the reference of System.web.Extensions.dll into your project.
// Pass the javascript object for conversion of object to JSON string
string jsondata = new JavaScriptSerializer().Serialize(obj);
string path = Server.MapPath("~/App_Data/");
// Write that JSON to txt file,
System.IO.File.WriteAllText(path + "output.json", jsondata);
//but if you want to paste it on specific drive then you can use following code
System.IO.File.Copy("sourcePath", "\\machinename\DriveLetter$\folder name");
//e.g. System.IO.File.Copy("~\AppData\output.json", "\\machinename\E$\test\test.txt")
you can try this tool that allows you to export SharePoint lists to JSON, XML, Collections Power Apps and more.