Create a gist now

Instantly share code, notes, and snippets.

Web page for reformatting JSON, implemented using AngularJS
<!DOCTYPE html>
<html lang="en" ng-app="formatterApp">
<head>
<meta charset="utf-8">
<title>JSON Reformatter</title>
<!--
Copyright (c) 2014 Kristopher Johnson
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<link href='//fonts.googleapis.com/css?family=Source+Sans+Pro:400,700|Source+Code+Pro' rel='stylesheet' type='text/css'>
<style>
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
padding: 1em;
line-height: 1;
font-family: 'Source Sans Pro', sans-serif;
}
h1 {
margin-bottom: .67em;
font-size: larger;
font-weight: bold;
}
p {
margin: 1em 0;
}
textarea {
border: 1px solid black;
font-family: 'Source Code Pro', monospace;
font-size: 10pt;
}
.small {
font-size: smaller;
}
.output-good {
color: green;
background-color: white;
}
.output-error {
color: white;
background-color: red;
}
</style>
</head>
<body>
<h1>JSON Reformatter</h1>
<form ng-controller="formatterController">
<textarea
ng-model="inputText"
id="inputTextarea"
rows="12"
cols="80"
placeholder="Paste your JSON here"
autofocus="true">
</textarea>
<br>
<button ng-click="clearInputText();">Clear Input Text</button>
<br>
<textarea
ng-bind="outputText"
ng-class="outputClass"
rows="12"
cols="80"
placeholder="Reformatted JSON will appear here"
readonly="true">
</textarea>
<br>
<label>
Indentation:
<select
ng-options="option.label for option in indentOptions"
ng-model="selectedIndentOption">
</select>
</label>
</form>
<p class="small">
For more about this webapp, see <a href="http://undefinedvalue.com/2014/05/28/web-page-reformatting-json-text-using-angularjs">A Web Page for Reformatting JSON Text, using AngularJS</a>
</p>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js"></script>
<script>
angular.module('formatterApp', [])
.controller('formatterController', ['$scope', '$window', function($scope, $window) {
$scope.inputText = '';
$scope.indentOptions = [
{label: 'None', value: 0 },
{label: 'One Space', value: 1 },
{label: 'Two Spaces', value: 2 },
{label: 'Three Spaces', value: 3 },
{label: 'Four Spaces', value: 4 },
{label: 'Eight Spaces', value: 8 },
{label: 'Tab', value: '\t'}
];
$scope.selectedIndentOption = $scope.indentOptions[2];
$scope.clearInputText = function() {
$scope.inputText = '';
$window.document.getElementById('inputTextarea').focus();
};
$scope.$watch('inputText', updateOutput);
$scope.$watch('selectedIndentOption', updateOutput);
function updateOutput() {
try {
var indent = $scope.selectedIndentOption.value;
$scope.outputText = formatJSON($scope.inputText, indent);
$scope.outputClass = 'output-good';
}
catch (err) {
$scope.outputText = err.message;
$scope.outputClass = 'output-error';
}
}
function formatJSON(input, indent) {
if (input.length == 0) {
return '';
}
else {
var parsedData = JSON.parse(input);
return JSON.stringify(parsedData, null, indent);
}
}
}]);
</script>
</body>
</html>
@kristopherjohnson

For a ReactJS implementation of this, see https://gist.github.com/kristopherjohnson/70d21a00e617f45be202

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment