um trying to obtain all the possible values that match my given regular expression. I have this long string which include the schema of a table. I need to obtain all the field names of the table. I tried following regular expression and while loop to iterate through the matcher. I can get the first column value from group(1) in the first iteration, but after that i am getting null.
String pattern = "\\},\"|\":\\{\"([^\"]+)\":\\{\"";
String schema = "{\"columns\":{\"scenario\":{\"type\":\"STRING\",\"isScoreParam\":false,\"isIndex\":false},\"_duration_1\":{\"type\":\"STRING\",\"isScoreParam\":false,\"isIndex\":true},\"query\":{\"type\":\"STRING\",\"isScoreParam\":false,\"isIndex\":false}},\"primaryKeys\":[]}";
Matcher m = Pattern.compile(pattern).matcher(schema);
while (m.find()) {
System.out.println("Found value: " + m.group(0) );
System.out.println("Found value: " + m.group(1));
}
Output of the above code is,
Found value: ":{"scenario":{"
Found value: scenario
Found value: },"
Found value: null
Found value: },"
Found value: null
Found value: },"
Found value: null
I need to filter out scenario, _duration_1, query from above string and insert it into an array. But i am still unclear how to do it. I am not sure whether it is possible to do that with regex. So, can someone please give me help in this. Thanks in advance...!!
Update 1
Well i tried the json parser with following code,
JSONObject json = (JSONObject)new JSONParser().parse("{\"columns\":{\"scenario\":{\"type\":\"STRING\",\"isScoreParam\":false,\"isIndex\":false},\"_duration_1\":{\"type\":\"STRING\",\"isScoreParam\":false,\"isIndex\":true},\"query\":{\"type\":\"STRING\",\"isScoreParam\":false,\"isIndex\":false}},\"primaryKeys\":[]}");
System.out.println(json.get("columns"));
Then um getting the output as,
{"_duration_1":{"isIndex":true,"isScoreParam":false,"type":"STRING"},"scenario":{"isIndex":false,"isScoreParam":false,"type":"STRING"},"query":{"isIndex":false,"isScoreParam":false,"type":"STRING"}}
But, what i want is to obtain those column names seperately into an array/list as
[scenario, _duration_1, query].
But it looks like i need to know the key values in order to obtain the value using json.get(key) method.
Update 2
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject)new JSONParser().parse("{\"columns\":{\"scenario\":{\"type\":\"STRING\",\"isScoreParam\":false,\"isIndex\":false},\"_duration_1\":{\"type\":\"STRING\",\"isScoreParam\":false,\"isIndex\":true},\"query\":{\"type\":\"STRING\",\"isScoreParam\":false,\"isIndex\":false}},\"primaryKeys\":[]}");
JSONObject keys = (JSONObject)json.get("columns");
System.out.println(keys.keySet());
Output,
[_duration_1, scenario, query]