// 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}`;
}