Json - Json From Javascript


Json - Json From Javascript

Sample Code :

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">
        var WikiTechy = { 
            websiteName: "www.wikitechy.com",
            type: "technology website", 
            current: "json basics", 
            author : "venkatesanprabu"
        };         
        varjsonText = JSON.stringify(WikiTechy); 
        document.writeln(jsonText);
    </script>
</head>
<body>
</body>
</html>

Code Explanation :

    The script element defines a javascript as its type by the code: <script type="text/javascript">

    A variable is declared with the name “WikiTechy” to contain data in the form of key value pairs to define a website name, its type, its current topic and author is declared as follows:

varWikiTechy = { 
    websiteName: "www.wikitechy.com",
    type: "technology website", 
    current: "json basics", 
    author : "venkatesanprabu"
};
    The code varjsonText = JSON.stringify(WikiTechy); converts the JSON variable WikiTechy into a JavaScript variable.

    The code document.writeln(jsonText); writes the content of the variable “jsonText” to the output window.

Sample Output :

The output is,

  • {"websiteName":"www.wikitechy.com","type":"technology-website","current":"json basics","author":"venkatesanprabu"}