Skip to content

Instantly share code, notes, and snippets.

@kiloreux
Created November 27, 2016 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiloreux/5eada9bf379d8fdaab9d56aeba8996a9 to your computer and use it in GitHub Desktop.
Save kiloreux/5eada9bf379d8fdaab9d56aeba8996a9 to your computer and use it in GitHub Desktop.
Turn JSON Object to Ruby hashes.
formatJR(obj, depth = 0) {
let out =""
let addition = ""
let steps = []
let type = typeof(obj)
switch(type) {
case "string":
case "boolean":
case "number":
out += JSON.stringify(obj)
break
case "object":
let isArray = Array.isArray(obj)
debugger
out+=" {\n"
depth += 1
for (let key in obj) {
out += " ".repeat(depth)
if(!isArray) {
out += `:${key}` + " =>"
}
if(Array.isArray(obj[key])) {
out += JSON.stringify(obj[key]) + ",\n"
}
else {
out += this.formatValue(obj[key], depth) + ",\n"
}
}
depth-=1
out += " ".repeat(depth) + "}"
break
default:
console.log("unknown type for formatting", type, obj)
}
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment