I have an ugly 1MB+ JSON object with numerous deep properties including nested arrays containing nested objects etc.
I'm looking for a function that can return an array of string "paths" for every property in a given object.
['obj.propA.first', 'obj.propA.second', 'obj.propB']
All of my searching thus far has turned up solutions going the other direction. Eg: Taking path strings and fetching the property values.
My gut says there has to be a better way than reinventing the wheel.
Thanks in advance!
Example behavior:
var ugly = {
a: 'a',
b: ['b1', 'b2'],
c: {
c1: 'c1',
c2: ['c2a', 'c2b'],
c3: {
c3a: 'c3a',
c3b: [['c3b']],
},
c4: [{c4a: 'c4a'}],
}
};
getPaths = function(obj) {
???
};
getPaths(ugly) = [
'a',
'b[0]',
'b[1]',
'c.c1',
'c.c2[0]',
'c.c2[1]',
'c.c3.c3a',
'c.c3.c3b[0][0]',
'c.c4[0].c4a',
];