0% found this document useful (0 votes)
13 views2 pages

JSON and CSV Conversion Functions

The document contains JavaScript functions for converting JSON to JavaScript objects, dates, and CSV formats, as well as converting CSV back to JSON. It also includes a function for creating a SHA-256 hash using the Web Crypto API. Each function handles errors by displaying appropriate messages for invalid inputs.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

JSON and CSV Conversion Functions

The document contains JavaScript functions for converting JSON to JavaScript objects, dates, and CSV formats, as well as converting CSV back to JSON. It also includes a function for creating a SHA-256 hash using the Web Crypto API. Each function handles errors by displaying appropriate messages for invalid inputs.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

// Convert JSON text to JavaScript object

function convertJsonToObject() {
const jsonInput = [Link]('jsonInput').value;
try {
const jsonObject = [Link](jsonInput);
[Link]('objectOutput').textContent =
[Link](jsonObject, null, 2);
} catch (error) {
[Link]('objectOutput').textContent = 'Invalid JSON!';
}
}

// Convert JSON date text to JavaScript Date object


function convertJsonToDate() {
const dateInput = [Link]('dateInput').value;
try {
const jsonDate = [Link](dateInput);
const dateObject = new Date([Link]);
[Link]('dateOutput').textContent = `Date Object: $
{dateObject}`;
} catch (error) {
[Link]('dateOutput').textContent = 'Invalid JSON or date
format!';
}
}

// Convert JSON to CSV


function convertJsonToCsv() {
const jsonCsvInput = [Link]('jsonCsvInput').value;
try {
const jsonArray = [Link](jsonCsvInput);
const headers = [Link](jsonArray[0]);
const csvRows = [
[Link](','),
...[Link](row => [Link](header => row[header]).join(','))
];
[Link]('csvOutput').textContent = [Link]('\n');
} catch (error) {
[Link]('csvOutput').textContent = 'Invalid JSON format!';
}
}

// Convert CSV to JSON


function convertCsvToJson() {
const csvToJsonInput = [Link]('csvToJsonInput').value;
const lines = [Link]('\n');
const headers = lines[0].split(',');
const jsonArray = [Link](1).map(line => {
const values = [Link](',');
return [Link]((object, header, index) => {
object[header] = values[index];
return object;
}, {});
});
[Link]('jsonOutput').textContent = [Link](jsonArray,
null, 2);
}

// Create a hash using crypto module


// Create a hash using the Web Crypto API (SHA-256)
async function createHash() {
const hashInput = [Link]('hashInput').value;
const encoder = new TextEncoder();
const data = [Link](hashInput);

// Use the Web Crypto API to generate a hash


const hashBuffer = await [Link]('SHA-256', data);

// Convert hash buffer to hex string


const hashArray = [Link](new Uint8Array(hashBuffer));
const hashHex = [Link](b => [Link](16).padStart(2, '0')).join('');

[Link]('hashOutput').textContent = `Hash: ${hashHex}`;


}

You might also like