exporting custom data structure
You could start with API Explorer v4:

Set the Entity to CustomGroup and Action to get.
In the Chain section, set Entity to CustomField and Action to get.  The next box should autopopulate to {where: [[custom_group_id, =, $id]]}

Basic example
From there, you can limit the fields returned both for the CustomGroup and the CustomFields and maybe sort them.  Once that shows what you want, remove the Limit so see all the data.
Example with selected fields
From there, you can copy/paste the generated code to a php file and do what you like with the output.