kishan
kishan

Reputation: 31

How to convert complicated nested list of map into json object

   List<Map<String, List<Map<String, Object>>>> ListMapTermMapList = new ArrayList<Map<String,List<Map<String ,Object>>>>();

Input

[
    {
        Inventories=
        [
        {FINANCIAL_BLOCK=Balance Sheet, COUNTRY_ID=8, TAXONOMY_ID=34, VALUE=279.7, COMPANY_STAGING_ID=9433, FINANCIAL_NAME=Inventories, COMPANY_CODE=LENZ, TYPE=companyFinal, COMPANY_ID=31, REPORTED_IN_FINANCIALS=Inventories, SUB_BLOCK=Current Assets, _version_=1559400052581990400}, {FINANCIAL_BLOCK=Balance Sheet, COUNTRY_ID=1, TAXONOMY_ID=34, VALUE=1122.0, COMPANY_STAGING_ID=102049, FINANCIAL_NAME=Inventories, COMPANY_CODE=APPLE, TYPE=companyFinal, COMPANY_ID=2, REPORTED_IN_FINANCIALS=Inventories, SUB_BLOCK=Current Assets, _version_=1559400052590379009}
        ]
    },  

    {
        High Performance Mixed Signal ( HPMS )=
        [
        {FINANCIAL_BLOCK=Income Statement, EXTENSION_NAME=Work performed by the Group and capitalized, COUNTRY_ID=8, TAXONOMY_ID=17602, VALUE=7.5, COMPANY_STAGING_ID=9564, FINANCIAL_NAME=Other Operating Income, COMPANY_CODE=LENZ, TYPE=companyFinal, COMPANY_ID=31, REPORTED_IN_FINANCIALS=Work performed by the Group and capitalized, SUB_BLOCK=Operating Block, _version_=1559400052606107648}, {FINANCIAL_BLOCK=Income Statement, EXTENSION_NAME=Work performed by the Group and capitalized, COUNTRY_ID=8, TAXONOMY_ID=17602, VALUE=30269.0, COMPANY_STAGING_ID=8926, FINANCIAL_NAME=Other Operating Income, COMPANY_CODE=LENZ, TYPE=companyFinal, COMPANY_ID=31, REPORTED_IN_FINANCIALS=Work performed by the Group and capitalized, SUB_BLOCK=Operating Block, _version_=1559400052663779330}
        ]
    }
]

I am trying to convert this complex nested list of map into JSON object using jackson mapper.

createJson(map);

The function for conversion is:

public static String createJson(Object obj) throws JsonGenerationException,JsonMappingException, IOException
{
    org.codehaus.jackson.map.ObjectMapper mapper = new org.codehaus.jackson.map.ObjectMapper();
    SimpleDateFormat sdf = new SimpleDateFormat(DF);
    mapper.getSerializationConfig().setDateFormat(sdf);
    String json = mapper.writeValueAsString(obj);
    return json;
}

Exception

org.codehaus.jackson.map.JsonMappingException: (was java.lang.UnsupportedOperationException) (through reference chain: java.util.ArrayList[0]->java.util.HashMap["Inventories"]->java.util.ArrayList[0])

Upvotes: 0

Views: 8864

Answers (1)

Mouad EL Fakir
Mouad EL Fakir

Reputation: 3749

I think Gson will save your life in this case :

Check this example of how to use it :

public static void main(String[] args)
{
    //Part 1
    Map<String ,Object> map1 = new HashMap<>();
    map1.put("FINANCIAL_BLOCK", "Balance Sheet");
    map1.put("COUNTRY_ID", 8);

    List<Map<String ,Object>> inventoriesList = new ArrayList<>();
    inventoriesList.add(map1);

    Map<String,List< Map<String ,Object> > > inventoriesMap = new HashMap<>();
    inventoriesMap.put("Inventories", inventoriesList);

    //Part 2
    Map<String ,Object> map2 = new HashMap<>();
    map2.put("FINANCIAL_BLOCK", "Income Statement");
    map2.put("COUNTRY_ID", 8);

    List<Map<String ,Object>> highPerformanceList = new ArrayList<>();
    highPerformanceList.add(map2);

    Map<String,List< Map<String ,Object> > > highPerformanceMap = new HashMap<>();
    highPerformanceMap.put("High Performance Mixed Signal ( HPMS )", highPerformanceList);

    //Collect
    List<Map<String, List<Map<String, Object>>>> ListMapTermMapList = new ArrayList< Map<String,List< Map<String ,Object> > > >();
    ListMapTermMapList.add(inventoriesMap);
    ListMapTermMapList.add(highPerformanceMap);

    //Format to Json
    System.out.println(new Gson().toJson(ListMapTermMapList));


}

Output :

[
    {
        "Inventories": [
            {
                "FINANCIAL_BLOCK": "Balance Sheet",
                "COUNTRY_ID": 8
            }
        ]
    },
    {
        "High Performance Mixed Signal ( HPMS )": [
            {
                "FINANCIAL_BLOCK": "Income Statement",
                "COUNTRY_ID": 8
            }
        ]
    }
]

Upvotes: 2

Related Questions