Java class JSONObject
:
I have a JSON object, from which I want to extract only some of the key/value pairs into another JSON object. In addition, I want to change the names of the extracted keys. And finally, I want to have the output object "flat" (i.e., all the elements in depth 1).
For example:
Input object:
{
"a":"val_a",
"b":
{
"b1":"val_b1",
"b2":"val_b2"
},
"c":
{
"c1":"val_c1",
"c2":
{
"c21":"val_c21",
"c22":"val_c22"
}
}
}
Output object:
{
"x":"val_a",
"y":"val_b1",
"z":"val_c22"
}
What is the best ("cleanest") way to implement this?
At present, I'm extracting the fields one by one "manually", like so (for the example above):
JSONObject output = new JSONObject();
output.put("x",input.getString("a"));
output.put("y",input.getJSONObject("b").getString("b1"));
output.put("z",input.getJSONObject("c").getJSONObject("c2").getString("c22"));
Is there some sort of "regular expression" that would help me to achieve this in "one shot"?
An answer on the given example would be highly appreciated.
Thanks