0

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]

udani
  • 1,243
  • 2
  • 11
  • 33
  • 4
    Why don't you just use JSON parser? – Doro Nov 09 '15 at 11:49
  • 1
    Using a JSON parser is the way to go since the solution would be more robust. If you feel that this is not what you need to do, please provide some more information as to what exactly, are you trying to achieve. – npinti Nov 09 '15 at 11:52
  • I tried the jason parser example from above link, but i am unable to obtain the values i need seperately because we need to know the key name in order to use the get method. But what i am trying to obtain is the key value itself. So is it possible to use the parser? – udani Nov 09 '15 at 12:15
  • I could get the keys as a set, from Update 2. I wish i asked this question earlier.... it could have saved my day. – udani Nov 09 '15 at 12:43
  • It happens to everyone, don't worry :). Glad your problem has been solved. – npinti Nov 09 '15 at 12:44
  • hehee...thanks alot... – udani Nov 09 '15 at 12:48

0 Answers0