Skip to content

Instantly share code, notes, and snippets.

@TechplexEngineer
Last active May 10, 2016 15:37
Show Gist options
  • Select an option

  • Save TechplexEngineer/e35da16a99debe3adb13b9e0f54215bb to your computer and use it in GitHub Desktop.

Select an option

Save TechplexEngineer/e35da16a99debe3adb13b9e0f54215bb to your computer and use it in GitHub Desktop.
Given "JSONlike" data this code adds the proper tabs and newlines

Given ugly json like data these programs add the proper tabs and line returns for nice formatting. Based on code https://tools.arantius.com/tabifier

Given input like this(A nested dictionary priettyprinted with pprint in python):

{   'name': '?',
	'path': '?',
	'hostname': 'B1-C1',
	'interfaces': [   {   'hw-id': '00:50:56:b9:76:04',
						  'ip': '10.1.2.1/24',
						  'name': 'eth1',
						  'vlan': '?'},
					  {   'hw-id': '00:50:56:b9:08:68',
						  'ip': '50.1.1.1/16',
						  'name': 'eth2',
						  'vlan': '?'},
					  {   'hw-id': '00:50:56:b9:16:85',
						  'ip': '10.1.50.1/24',
						  'name': 'eth3',
						  'vlan': '?'},
					  {   'hw-id': '00:50:56:b9:4e:c7',
						  'ip': '10.1.1.1/24',
						  'name': 'eth0',
						  'vlan': '?'}],
	'configure': {'netflow': '?', 'ospf': '?'}}]

Theese programs give output like this:

[
	{
		'name': '?',
		'path': '?',
		'hostname': 'B1-C1',
		'interfaces': [
			{
				'hw-id': '00:50:56:b9:76:04',
				'ip': '10.1.2.1/24',
				'name': 'eth1',
				'vlan': '?'
			},
			{
				'hw-id': '00:50:56:b9:08:68',
				'ip': '50.1.1.1/16',
				'name': 'eth2',
				'vlan': '?'
			},
			{
				'hw-id': '00:50:56:b9:16:85',
				'ip': '10.1.50.1/24',
				'name': 'eth3',
				'vlan': '?'
			},
			{
				'hw-id': '00:50:56:b9:4e:c7',
				'ip': '10.1.1.1/24',
				'name': 'eth0',
				'vlan': '?'
			}
		],
		'configure': {
			'netflow': '?',
			'ospf': '?'
		}
	}
]
// Based on code https://tools.arantius.com/tabifier
function cleanJson(code) {
var level = 0;
function tabs() {
var s = '';
for (var j = 0; j < level; j++)
s += '\t';
return s;
}
code = code.replace(/^[\s\n]*/, ''); //leading space
code = code.replace(/[\s\n]*$/, ''); //trailing space
code = code.replace(/[\n\r]+/g, '\n'); //collapse newlines
var out = tabs(),
c = '';
var instring = false;
for (var i=0; i < code.length; i++) {
c = code.charAt(i);
if (instring) {
if (instring == c && // this string closes at the next matching quote
// unless it was escaped, or the escape is escaped
('\\' != code.charAt(i - 1) || '\\' == code.charAt(i - 2))
) {
instring = false;
}
out += c;
} else if ('"' == c || "'" == c) {
if (instring && c == instring) {
instring = false;
} else {
instring = c;
}
out += c;
} else if ('{' == c || '[' == c) {
level++;
out += c + '\n' + tabs();
while (code.charAt(++i).match(/\s/));
i--;
} else if ('}' == c || ']' == c) {
out = out.replace(/\s*$/, '');
level--;
if (!out.match(/(\{|\[)$/)) out += '\n' + tabs();
out += c;
while (code.charAt(++i).match(/\s/));
i--;
} else if (',' == c) {
out += ',\n' + tabs();
while (code.charAt(++i).match(/\s/));
i--;
} else {
out += c;
}
}
out = out.replace(/[\s\n]*$/, '');
out = out.replace(/\n\s*\n/g, '\n'); //blank lines
out = out.replace(/^[\s\n]*/, ''); //leading space
out = out.replace(/[\s\n]*$/, ''); //trailing space
return out;
}
# Based on code https://tools.arantius.com/tabifier
def json(code):
level = 0;
def tabs():
s = '';
for j in range(0, level):
s += '\t'
return s
import re
code = re.sub(re.compile(r"^[\s\n]*", re.MULTILINE), '', code) #leading space
code = re.sub(re.compile(r"[\s\n]*$", re.MULTILINE), '', code) #trailing space
code = re.sub(re.compile(r"[\n\r]*", re.MULTILINE), '', code) #collapse newlines
out = tabs()
c = ''
instring = False
for i in range(0, len(code)):
c = code[i];
if (instring):
# this string closes at the next matching quote
# unless it was escaped, or the escape is escaped
if (instring == c and ('\\' != code[i - 1] or '\\' == code[i - 2])):
instring = False;
out += c;
elif ('"' == c or "'" == c):
if (instring and c == instring):
instring = False;
else:
instring = c;
out += c;
elif ('{' == c or '[' == c):
level +=1
out += c + '\n' + tabs();
i+=1
while (re.match(r"\s", code[i])): ##note i+=1 was ++i
i+=1
i-=1
elif ('}' == c or ']' == c):
out = re.sub(r"\s*$", '', out)
level -=1;
if (not re.match(r"(\{|\[)$", out)):
out += '\n' + tabs();
out += c;
while (re.match(r"\s",code[i])): ##note i+=1 was ++i
i+=1
i-=1
elif (',' == c):
out += ',\n' + tabs();
# out += "-COMMA-"
i+=1
while (re.match(r"\s", code[i])): ##note i+=1 was ++i
i+=1
i-=1
else:
out += c
# out = re.sub(re.compile(r"[\s\n]*$", re.MULTILINE), '', out)
# out = re.sub(re.compile(r"\n\s*\n", re.MULTILINE), '\n', out) #blank lines
# out = re.sub(re.compile(r"^[\s\n]*", re.MULTILINE), '', out) #leading space
# out = re.sub(re.compile(r"[\s\n]*$", re.MULTILINE), '', out) #trailing space
return out
if __name__ == '__main__':
data = "[ { 'name': '?',\
'path': '?',\
'hostname': 'B1-C1',\
'interfaces': [ { 'hw-id': '00:50:56:b9:76:04',\
'ip': '10.1.2.1/24',\
'name': 'eth1',\
'vlan': '?'},\
{ 'hw-id': '00:50:56:b9:08:68',\
'ip': '50.1.1.1/16',\
'name': 'eth2',\
'vlan': '?'},\
{ 'hw-id': '00:50:56:b9:16:85',\
'ip': '10.1.50.1/24',\
'name': 'eth3',\
'vlan': '?'},\
{ 'hw-id': '00:50:56:b9:4e:c7',\
'ip': '10.1.1.1/24',\
'name': 'eth0',\
'vlan': '?'}],\
'configure': {'netflow': '?', 'ospf': '?'}}]\
";
print(json(data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment