"Does anyone know of a better or more generic way to do these kind of things?"
There's lots of choices for config files. JSON and YAML are popular. Your second example is pretty close to JSON already. It would look like this as JSON:
There are perl modules to parse JSON, some streaming, if you really can't load it all into memory. There's also a really nice command line utility called "jq", see some examples here.
Note that JSON doesn't support comments, which is probably the biggest complaint about it as a configuration file format.