Prettify json data in textarea input - Javascript JSon

Javascript examples for JSon:JSon Object

Description

Prettify json data in textarea input

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
function prettyPrint() {//ww w .j av  a  2s  .com
    var ugly = document.getElementById('myTextArea').value;
    var obj = JSON.parse(ugly);
    var pretty = JSON.stringify(obj, undefined, 4);
    document.getElementById('myTextArea').value = pretty;
}

      </script> 
   </head> 
   <body> 
      <textarea id="myTextArea" cols="50" rows="10"></textarea> 
      <button onclick="prettyPrint()">Pretty Print</button>  
   </body>
</html>

Related Tutorials