You can use library Josson to do the job.
https://github.com/octomix/josson
Your example is an JSON object
Josson object = Josson.fromJsonString(
"{" +
" \"id\":237," +
" \"first_name\":\"LeBron\"," +
" \"last_name\":\"James\"," +
" \"position\":\"F\"," +
" \"height_feet\": 6," +
" \"height_inches\": 8," +
" \"weight_pounds\": 250," +
" \"team\":{" +
" \"id\":14," +
" \"abbreviation\":\"LAL\"," +
" \"city\":\"Los Angeles\"," +
" \"conference\":\"West\"," +
" \"division\":\"Pacific\"," +
" \"full_name\":\"Los Angeles Lakers\"," +
" \"name\":\"Lakers\"" +
" }" +
"}");
Transformation
String keys = object.getString("flatten('.','[%d]').keys().csv()");
System.out.println(keys);
String values = object.getString("flatten('.','[%d]').csv()");
System.out.println(values);
Output
id,first_name,last_name,position,height_feet,height_inches,weight_pounds,team.id,team.abbreviation,team.city,team.conference,team.division,team.full_name,team.name
237,LeBron,James,F,6,8,250,14,LAL,Los Angeles,West,Pacific,Los Angeles Lakers,Lakers
If the input is an JSON array
Josson array = Josson.fromJsonString(
"[{" +
" \"id\":237," +
" \"first_name\":\"LeBron\"," +
" \"last_name\":\"James\"," +
" \"position\":\"F\"," +
" \"height_feet\": 6," +
" \"height_inches\": 8," +
" \"weight_pounds\": 250," +
" \"team\":{" +
" \"id\":14," +
" \"abbreviation\":\"LAL\"," +
" \"city\":\"Los Angeles\"," +
" \"conference\":\"West\"," +
" \"division\":\"Pacific\"," +
" \"full_name\":\"Los Angeles Lakers\"," +
" \"name\":\"Lakers\"" +
" }" +
"}," +
"{" +
" \"id\":888," +
" \"first_name\":\"Anthony\"," +
" \"last_name\":\"Davis\"," +
" \"position\":\"F\"," +
" \"height_feet\": 6," +
" \"height_inches\": 10," +
" \"weight_pounds\": 253," +
" \"team\":{" +
" \"id\":14," +
" \"abbreviation\":\"LAL\"," +
" \"city\":\"Los Angeles\"," +
" \"conference\":\"West\"," +
" \"division\":\"Pacific\"," +
" \"full_name\":\"Los Angeles Lakers\"," +
" \"name\":\"Lakers\"" +
" }" +
"}]");
Transformation
String keys = array.getString("[0].flatten('.','[%d]').keys().csv()");
System.out.println(keys);
String values = array.getString("[]@.flatten('.','[%d]').csv().@join('\n')");
System.out.println(values);
Output
id,first_name,last_name,position,height_feet,height_inches,weight_pounds,team.id,team.abbreviation,team.city,team.conference,team.division,team.full_name,team.name
237,LeBron,James,F,6,8,250,14,LAL,Los Angeles,West,Pacific,Los Angeles Lakers,Lakers
888,Anthony,Davis,F,6,10,253,14,LAL,Los Angeles,West,Pacific,Los Angeles Lakers,Lakers