Skip to content

Instantly share code, notes, and snippets.

@huttj
Created April 6, 2016 20:39
Show Gist options
  • Save huttj/02ad59cd049c2312d86a5811a3a7723b to your computer and use it in GitHub Desktop.
Save huttj/02ad59cd049c2312d86a5811a3a7723b to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<style>
textarea {
width: calc(100% - .5em);
height: 50vh;
}
pre {
width: calc(100% - 2em);
border: 1px solid #ccc;
padding: 1em;
border-radius: 6px;
}
</style>
</head>
<body>
<textarea id="input" cols="30" rows="10"></textarea>
<pre id="output"></pre>
<script type="text/javascript">
var input = document.getElementById('input');
var output = document.getElementById('output');
input.addEventListener('change', handler);
input.addEventListener('keyup', handler);
function handler(e) {
var result;
try {
result = JSON.stringify(jsonToSchema(JSON.parse(e.target.value)), null, 2);
} catch (e) {
result = 'Error: ' + e.message;
}
output.textContent = result;
}
function jsonToSchema(obj) {
var type = typeof obj;
switch (type) {
case 'object':
for (var key in obj) {
obj[key] = jsonToSchema(obj[key]);
}
return obj;
default:
return type;
}
}
</script>
</body>
</html>
@huttj
Copy link
Author

huttj commented Apr 6, 2016

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