Search code examples
javajsonresultset

mapping resultset to json


I have some resultset data as follows :

+-------+-------+-------+-------+
| Code1 | Code2 | Code3 | Code4 |
+-------+-------+-------+-------+
|     1 |    11 |   111 |  1111 |
|     2 |    21 |   211 |  2111 |
|     2 |    22 |   221 |  2211 |
|     2 |    21 |   212 |  2121 |
+-------+-------+-------+-------+

I need the above result set to be converted to following two jsons.

code1_code2 = {
               1 : [11],
               2 : [21, 22, 21]
              };

code2_code3 = {
               11 : [111],
               21 : [211, 212],
               22 : [221]
              };

i have tried parsing the result set and as the 1st column is ordered i could get the 1st json. But as the second column is not ordered i couldnot get the 2nd json.

(Note:- i would not be getting the 2nd column ordered)


Solution

  • NOTE

    The name in name/value pair of JSON should be string (not a number in your question).

    JSON Object
    (source: json.org)

    Sample code

    As mentioned in @josnidhin 's comment, you can use Map to store these data no matter if the result set is ordered or not.

    Here's a sample code, I choose json-lib to handle JSON stuff.

    package test;
    
    import java.sql.*;
    import java.util.*;
    import java.util.concurrent.*;
    
    import net.sf.json.*;
    
    public class StackOverflowQ10976771_ResultSetToJSON
    {
        public static void appendValue (Map<String,  List<Integer>> map, String key, int value)
        {
            List<Integer> values = map.get (key);
            if (values == null)
            {
                values = new ArrayList<Integer> ();
                map.put (key, values);
            }
            values.add (value);
        }
    
        public static void main (String[] args)
        {
            Map<String, List<Integer>> code1_code2 = new ConcurrentSkipListMap<String, List<Integer>> ();
            Map<String, List<Integer>> code2_code3 = new ConcurrentSkipListMap<String, List<Integer>> ();
            Map<String, List<Integer>> code3_code4 = new ConcurrentSkipListMap<String, List<Integer>> ();
    
            int[][] sample_resultSet = {
                    {1, 11, 111, 1111},
                    {2, 21, 211, 2111},
                    {2, 22, 221, 2211},
                    {2, 21, 212, 2121},
            };
    
            //ResultSet rs = null;
            //while (rs.next ())
            for (int[] rs : sample_resultSet)
            {
                appendValue (code1_code2, String.valueOf(rs[0]), rs[1]);
                appendValue (code2_code3, String.valueOf(rs[1]), rs[2]);
                appendValue (code3_code4, String.valueOf(rs[2]), rs[3]);
            }
    
            System.out.println ("code1_code2 =");
            System.out.println (JSONObject.fromObject (code1_code2).toString(4 ,4) + ";");
            System.out.println ();
    
            System.out.println ("code2_code3 = ");
            System.out.println (JSONObject.fromObject (code2_code3).toString(4 ,4) + ";");
            System.out.println ();
    
            //System.out.println ("code3_code4 = ");
            //System.out.println (JSONObject.fromObject (code3_code4).toString(4 ,4) + ";");
            //System.out.println ();
        }
    }
    

    Sample output

    code1_code2 =
        {
            "1": [11],
            "2":         [
                21,
                22,
                21
            ]
        };
    
    code2_code3 = 
        {
            "11": [111],
            "21":         [
                211,
                212
            ],
            "22": [221]
        };