0

In the below query

SELECT grouped_target_tuple FROM targeting_template_target  WHERE targeting_template_id='5863' AND target_id="9906"

[{"childTargets":[{"id":472,"name":"AutoCountry","type":"multiselect","rank":1,"selectedTargetOptions":[{"id":322,"name":"Germany","excluded":false},{"id":323,"name":"Italy","excluded":false}]},{"id":473,"name":"AutoCity","type":"multiselect","rank":2,"selectedTargetOptions":[{"id":324,"name":"Newyork","excluded":false},{"id":325,"name":"NewJersey","excluded":false}]}]}]

How to fetch from the result set?

// Get TT id

        String query = "SELECT grouped_target_tuple FROM targeting_template_target  WHERE targeting_template_id='"
                + ttID + " AND target_id=" + groupedtarID1 + "';";
        ResultSet rs = getInstanceUtilsClass(MySQLUtilities.class).executeQuery(query, dbname);
        ResultSetMetaData rsmd = rs.getMetaData();
        JSONArray array = new JSONArray();
        JSONArray json = new JSONArray();
        while (rs.next()) {
            int numColumns = rsmd.getColumnCount();
            JSONObject obj = new JSONObject();
            for (int i = 1; i <numColumns; i++) {
                String column_name = rsmd.getColumnName(i);
                obj.put(column_name, rs.getObject(column_name));
            }
            json.put(obj);
        }
        BasePage.log(json.toString());
    }

The above is not working.

After I fetch, I want to iterate and perform assertion.

enter image description here

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • If the column is a [`JSON`](https://dev.mysql.com/doc/refman/8.0/en/json.html) datatype, then you can use [`rs.getString(column_name)`](https://stackoverflow.com/questions/43283689/how-to-read-json-data-type-in-mysql-using-jdbc). Then you can convert the string to a [`org.json.JSONObject`](https://stackoverflow.com/questions/5245840/how-to-convert-jsonstring-to-jsonobject-in-java). – andrewJames Sep 05 '20 at 19:15
  • Also, it is generally better (safer, and potentially more efficient) to use a [prepared statement](https://stackoverflow.com/questions/24988867/when-should-i-use-prepared-statements) to build your query, instead of using string concatenation. – andrewJames Sep 05 '20 at 19:22

0 Answers0