# JSON formatting and processing hierarchies

I have a properties file I'm using for GIS software. You have a feature like a road, then properties under it, like so:

{
"colour": "rgb(0,0,0)"
},
"railway": {
"colour": "rgb(100,100,100)"
}
}


Now I need to group these into categories, for example, transportation. The first thing I thought of was:

{
"transportation":
{
"colour": "rgb(0,0,0)"
},
"railway": {
"colour": "rgb(100,100,100)"
}
}
}


This makes things awkward when I want to access "road" (for example), because now I need to know it is under "transportation" (mind you I could always write a helper function to eliminate this). Further, "road" could be under multiple groups, perhaps "transportation" and "public works". Now I would need to duplicate the properties in both instances.

Would I be wiser to use the following format, or is there a better way to do this?

{
"colour": "rgb(0,0,0)",
"group": ["transportation"]
},
"railway": {
"colour": "rgb(100,100,100)",
"group": ["transportation"]
}
}

• This has nothing at all to do with php. – Theodore R. Smith Feb 5 '12 at 0:26
• It has little to do with PHP. The PHP portion is stating what language I am using to work with the data. People often ask stuff like "In what application are you trying to do this in?" Part of that question is answered by having the php tag. – Jason McCarrell Feb 8 '12 at 20:51

Assuming you're using JavaScript to manipulate the object, you could also split it up into 2 different structures, one for maintaining the hierarchy and another for easy access:

var hierarchy = {
transportation: {
colour: "rgb(0,0,0)"
},
railway: {
colour: "rgb(100,100,100)"
}
}
};

var features = {