JSFiddle - React, Tailwind, and code Playground
HTML
<div id="result"></div>
JavaScript
var input = {
"store": {
"book": [{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
}, {
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
};
var keys = [];
function recursiveParser(obj) {
if (!obj) {
console.log(path);
return;
}
var path = "";
if (obj.constructor == Array) { //if it's an array than parse every element of it
for (var i = 0; i < obj.length; i++) {
console.log(obj[i]);
recursiveParser(obj[i]);
}
} else if (obj.constructor == Object) { //if it's json
for (var key in obj) { //for each key
path += key.toString() + ".";
if (keys.indexOf(key) === -1) { // if you don't have it
keys.push(key); //store it
recursiveParser(obj[key]); //give the value of the key to the parser
} else {
recursiveParser(obj[key]); //if you do have it pass the value of the key anyway to the parser
}
}
console.log(path);
} else {
}
}
recursiveParser(input);
$('#result').text(keys);