Skip to content

Instantly share code, notes, and snippets.

@lynsei
Forked from zz85/print.js
Last active November 24, 2020 20:27
Show Gist options
  • Save lynsei/9e77fbd42bca2c05e13571fef7829e70 to your computer and use it in GitHub Desktop.
Save lynsei/9e77fbd42bca2c05e13571fef7829e70 to your computer and use it in GitHub Desktop.
Pretty Print JSON in Ascii Tree
// TODO add colors
sample = {
"language": {
"backend": "node.js",
"middleware": "node.js",
"frontend": "es2018"
},
"cloud-storage": {
"block": {
"aws": "ebs",
"gcp": "cbs",
"azure": "abs"
},
"ssd": {
"aws": "cost-prohibitive",
"gcp": "gce-ssd",
"azure": "cost-prohibitive"
},
"nvme": {
"aws": "cost-prohibitive",
"gcp": "nvme",
"azure": "cost-prohibitive"
}
},
"cloud-datastores": {
"nosql": {
"firebase": {},
"dynamodb": {},
"cosmos": {}
}
},
"hosted-datastore": {
"nosql": {
"mongodb": {},
"rethinkdb": {},
"couchdb": {}
},
"inmemory": {
"redis": {},
"hazelcast": {},
"VoltDB": {}
},
"static": {
"replicated": {},
"cdn-edge": {},
"pwa-offline": {}
}
},
"cloud-database": {
"aws": {
"mysql": "rds",
"postgre": "rds",
"mssql": "rds"
},
"gcp": {
"mysql": "cloudsql",
"postgre": "cloudsql",
"mssql": "cloudsql"
},
"azure": {
"mysql": "sqlservice",
"postgre": "sqlservice",
"mssql": "sqlservice"
}
},
"hosted-database": {
"mysql": {},
"postgre": {},
"mssql": {},
"mariadb": {}
},
"middleware": {
"logging": {
"morgan": {},
"graylog": {},
"winston": {}
},
"tracing": {
"zipkin": {},
"signalfx": {},
"jaeger": {}
},
"auth": {
"google": "firebase",
"auth0": "auth0",
"aws": "amplify"
}
},
"frameworks-ui": {
"framework": ["vue","react","ember"],
"components": [ "vuetify", "bootstrap", "polymer", "material", "semantic-ui"]
},
"frameworks-fullstack": {
"redwood": "redwoodJs",
"nuxt": "nuxtJs",
"feathers": "feathersJs"
},
"integrations-marketing": {
"email": ["maichimp", "klaviyo", "snowvio"],
"voice-ivr": ["vonage","twilio"],
"crm": ["salesforce","pipedrive","activecampaign"],
"content-curation": ["intelligently.social", "contentstudio", "crowdfire"],
"intelligence": ["spokeo", "crunchbase", "hunter.io"],
"chatbot": ["kommunicate","intercom","crisp"],
"customAI": ["dialogueFlow", "lex", "personalizer"]
},
"developer-tools": {
"api-generators": ["ibm-api-connect","swagger",]
}
}
const asciitree = require('ascii-tree');
function pretty_json(v) {
console.log(JSON.stringify( v, 0, 2 ))
}
function ascii_json(json) {
const list = ['#root'];
_traverse(list, json, 1);
const tree = asciitree.generate(list.join('\r\n'));
console.log(tree)
}
function _traverse(list, node, level) {
level++;
const prefix = Array(level + 1).join('#');
const type = typeof node;
if (node === undefined || node === null) {
// Array.isArray([])?
list.push(prefix + node);
}
else if (type === 'object') {
Object.keys(node).forEach(k => {
list.push(prefix + k);
_traverse(list, node[k], level);
});
}
else {
// string boolean
list.push(prefix + node);
}
}
pretty_json(sample)
ascii_json(sample)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment