How To Flatten Up Embedded Json Into Multiple Documents
Consider the following JSON: { 'Company' : 'ABC Company', 'Place' : { 'Bangalore' :{ 'Address' : 'MG Road',
Solution 1:
There you go : (jsb)
var t = [];
for (p in a.Place)
{
var _=a.Place[p]["Phone"];
for (i = 0; i < _.length; i++)
{
var g = {
Company: a.Company,
Place: p,
Address: a.Place[p]["Address"]
};
g.Phone = _[i];
t.push(g)
}
}
If you add
console.log(JSON.stringify(t)
you'll get this
[{"Company":"ABC Company","Place":"Bangalore","Address":"MG Road","Phone":"988888"},{"Company":"ABC Company","Place":"Bangalore","Address":"MG Road","Phone":"888866"},{"Company":"ABC Company","Place":"Bangalore","Address":"MG Road","Phone":"365656"},{"Company":"ABC Company","Place":"Mubmai","Address":"1st Main Road,West","Phone":"21212"},{"Company":"ABC Company","Place":"Mubmai","Address":"1st Main Road,West","Phone":"123123"},{"Company":"ABC
Company","Place":"Mubmai","Address":"1st Main Road,West","Phone":"544455"}]
Post a Comment for "How To Flatten Up Embedded Json Into Multiple Documents"