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

Program 7

The document provides an example of using JSON in web development, demonstrating various operations such as converting JSON to a JavaScript object, formatting dates, and converting JSON to CSV. It also includes a simple hashing function to create a hash from a string. The content is structured with HTML elements and JavaScript for functionality.

Uploaded by

flytechi13579
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)
3 views2 pages

Program 7

The document provides an example of using JSON in web development, demonstrating various operations such as converting JSON to a JavaScript object, formatting dates, and converting JSON to CSV. It also includes a simple hashing function to create a hash from a string. The content is structured with HTML elements and JavaScript for functionality.

Uploaded by

flytechi13579
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
You are on page 1/ 2

<!

DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON and Crypto Example</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
h2 { color: #333; }
</style>
</head>
<body>

<h2>JSON and Crypto Operations</h2>

<!-- JSON to Object -->


<div>
<h3>Convert JSON to JavaScript Object:</h3>
<p id="jsonToObj"></p>
</div>

<!-- JSON Date Conversion -->


<div>
<h3>Convert JSON Date:</h3>
<p id="jsonDate"></p>
</div>

<!-- JSON to CSV and CSV to JSON -->


<div>
<h3>JSON to CSV:</h3>
<p id="jsonToCsv"></p>
</div>

<div>
<h3>CSV to JSON:</h3>
<p id="csvToJson"></p>
</div>

<!-- Hashing a String -->


<div>
<h3>Create Hash from String:</h3>
<p id="hashOutput"></p>
</div>

<script>
// Sample JSON string
const jsonString = '{"name": "John", "age": 30, "dateOfBirth": "1994-01-01"}';

// a) Convert JSON text to JavaScript Object


const jsonObject = JSON.parse(jsonString);
document.getElementById("jsonToObj").innerText = JSON.stringify(jsonObject);

// b) Convert JSON results into a date


const date = new Date(jsonObject.dateOfBirth);
document.getElementById("jsonDate").innerText = date.toDateString();

// c) Converting From JSON To CSV


function jsonToCsv(json) {
const items = typeof json ! == 'object' ? JSON.parse(json) : json;
const header = Object.keys(items);
const csv = header.join(',') + '\n' + header.map(field =>
items[field]).join(',');
return csv;
}

const csvOutput = jsonToCsv(jsonObject);


document.getElementById("jsonToCsv").innerText = csvOutput;

// d) Create hash from string (using a simple hash function for demo)
function simpleHash(str) {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash += str.charCodeAt(i);
}
return hash.toString();
}

const hash = simpleHash("Hello World");


document.getElementById("hashOutput").innerText = `Hash of 'Hello World': ${hash}`;

</script>

</body>
</html>``````````````````````````````````````````````````````````````````

You might also like