i am trying to flatten the following JSON file into an array:
{
"@context": "http://schema.org",
"@type": "EventReservation",
"reservationNumber": "IO12345",
"underName": "John Smith",
"reservationFor": [{
"@type": "Event",
"name": "Google I/O 2013",
"startDate": "2013-05-15T08:30:00-08:00",
"location": "Moscone Center, 800 Howard St., San Francisco, CA 94103"
}, {
"@type": "Event",
"name": "Apple I/O 2013",
"startDate": "2016-05-15T08:30:00-08:00",
"location": "Evergllades Center, 800 Howard St., San Francisco, CA 94103"
}, {
"@type": "Event",
"name": "Apple I/O 2013",
"startDate": "2016-05-15T08:30:00-08:00",
"location": "Evergllades Center, 800 Howard St., San Francisco, CA 94103"
}]
}
I want to produce checkboxes with this array.
The problem is I get array indices like this: @[email protected]@Event.location
which should rather look like:
@[email protected]@Event.location
And I don't understand why this is the case. My code looks like this:
function toArray(obj, namespace) {
var result = [];
var array = obj instanceof Array;
namespace += !array ? "@" + obj["@type"] + "." : "@Array.";
for (var prop in obj) {
if(prop == "@context" || prop == "@type") continue;
var value = obj[prop];
if (typeof value === 'object') {
namespace += prop;
toArray(value, namespace).forEach(function(element) {
result.push(element);
});
}
else {
result.push(namespace + prop);
}
}
return result;
}
I am trying around for hours now and i can't figure out a solution so i would appreciate your help a lot. :D