Skip to content

Instantly share code, notes, and snippets.

@Zazza
Created February 14, 2021 06:36
Show Gist options
  • Save Zazza/d34955edd7b247dd0c27b928adfe5ea8 to your computer and use it in GitHub Desktop.
Save Zazza/d34955edd7b247dd0c27b928adfe5ea8 to your computer and use it in GitHub Desktop.
JS class ParseJSon
'use strict';
class ParseJson {
obj = {}
result = []
constructor(obj) {
this.obj = JSON.parse(obj)
}
parseObject() {
for (let value of this.inObject(this.obj, ''))
this.result[this.result.length] = value
return this.result
}
*inObject(obj, path) {
for (let item in obj) {
if (!Array.isArray(obj[item])) {
yield* this.inObject(obj[item], path + '.' + item)
} else {
yield path + '.' + item
}
}
}
}
const input = '{"bookkeeping":{"contacts": {"pones":[123456789,234567890,345678901],"emails":["[email protected]"]},"staff": {"total":2,"employees":{"full_time":[{"name":"Sonoo","email":"[email protected]","salary":56000,"married":true},{"name":"Ram","email":"[email protected]","salary":65000,"married":true},{"name":"Bob","email":"[email protected]","salary":42000,"married":true}]}}}}'
let parseJson = new ParseJson(input)
parseJson.parseObject()
console.log(parseJson.result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment