JSFiddle - React, Tailwind, and code Playground

by Haze32

HTML

<button onclick="downloadCSV()">Download CSV</button>

JavaScript

// Function to create CSV content from JSON data
        function jsonToCSV(json) {
            // Define the desired key order
            var keys = ['Title', 'TimeModified', 'MimeType', 'CollectionType', 'DocumentType', 'Author', 'BodyType', 'HW_OID'];
            var csv = keys.join(',') + '\n'; // Create CSV header in the specified order
            var cutoffDate = new Date("2024-04-01"); // Set the cutoff date to April 2024

            // Add CSV rows
            for (var obj in json.objects) {
                if (json.objects.hasOwnProperty(obj)) {
                    var modifiedDate = new Date(json.objects[obj]['TimeModified']);
                    if (modifiedDate < cutoffDate) { // Check if the date is before April 2024
                        var row = [];
                    keys.forEach(function (key) {
                        var value = json.objects[obj][key] || 'NULL'; // Fill missing keys with 'NULL'
                        if (key === 'Title' && value.startsWith("en:")) { // Remove "en:" from 'Title'
                            value = value.slice(3);
                            }
                            row.push(value); // Add the value to the row in the correct order
                        });
                        csv += row.join(',') + '\n'; // Add the row to the CSV content
                    }
                }
            }

            return csv; // Return the CSV content
        }

// Sample JSON data
var jsonResult = {
  "error": {},
  "objects": {
    "0": {
      "Author": "wklus_sys",
      "CollectionType": "Collection",
      "DocumentType": "collection",
      "GOid": "0x0acc35ee 0x0191b4c0",
      "HW_AdminAccess": "granted",
      "HW_AutomaticVersionControl": "false",
      "HW_ChildAccess": "UNLINK_ACCESS",
      "HW_CompoundSearchable": "true",
      "HW_EffectiveAccess": "WRITE_ACCESS",
      "HW_HideFromSearch": "yes",
      "HW_OID": "ID-12931.6984062325458-1",
      "HW_ObjectName": "Concepts",
     ...