What is the easy way to get all paths in the Given Json Object; For Example:
{
app:{
profiles:'default'
},
application:{
name:'Master Service',
id:'server-master'
},
server:{
protocol:'http',
host:'localhost',
port:8098,
context:null
}
}
I should able to produce the following object
app.profiles=default
application.name=Master Service
application.id=server-master
I was able to achieve the same using a recursive function. I want to know is there any built in function from json which does this.
You can implement your custom converter by iterating through objects recursively.
Something like this:
var YsakON = { // YsakObjectNotation
stringify: function(o, prefix) {
prefix = prefix || 'root';
switch (typeof o)
{
case 'object':
if (Array.isArray(o))
return prefix + '=' + JSON.stringify(o) + '\n';
var output = "";
for (var k in o)
{
if (o.hasOwnProperty(k))
output += this.stringify(o[k], prefix + '.' + k);
}
return output;
case 'function':
return "";
default:
return prefix + '=' + o + '\n';
}
}
};
var o = {
a: 1,
b: true,
c: {
d: [1, 2, 3]
},
calc: 1+2+3,
f: function(x) { // ignored
}
};
document.body.innerText = YsakON.stringify(o, 'o');
That's not a best converter implementation, just a fast-written example, but it should help you to understand the main principle.
Here is the working JSFiddle demo.